{"id":13537058,"url":"https://github.com/hadilq/log4k","last_synced_at":"2025-03-27T01:31:16.353Z","repository":{"id":50278889,"uuid":"290763121","full_name":"hadilq/log4k","owner":"hadilq","description":"This is a simple library for logging in Kotlin. https://proandroiddev.com/logging-in-kotlin-8ecfb97a4840","archived":false,"fork":false,"pushed_at":"2021-01-02T19:43:56.000Z","size":339,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T21:06:17.653Z","etag":null,"topics":["kotlin-multiplatform","log","log4j","log4k","logging","track","tracking"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hadilq.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-27T11:58:06.000Z","updated_at":"2024-11-05T15:05:11.000Z","dependencies_parsed_at":"2022-08-30T08:31:48.182Z","dependency_job_id":null,"html_url":"https://github.com/hadilq/log4k","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadilq%2Flog4k","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadilq%2Flog4k/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadilq%2Flog4k/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadilq%2Flog4k/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hadilq","download_url":"https://codeload.github.com/hadilq/log4k/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245764656,"owners_count":20668456,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kotlin-multiplatform","log","log4j","log4k","logging","track","tracking"],"created_at":"2024-08-01T09:00:54.274Z","updated_at":"2025-03-27T01:31:14.574Z","avatar_url":"https://github.com/hadilq.png","language":"Kotlin","funding_links":[],"categories":["Libraries"],"sub_categories":["Debug"],"readme":" ![Health Check](https://github.com/hadilq/log4k/workflows/Health%20Check/badge.svg?branch=master)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.hadilq/log4k/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.hadilq/log4k)\n\nLog4k\n---\nThis is a simple library for logging in Kotlin. It's inspired by the famous Apache Log4j library. You can find the original repository that I pushed this library at https://github.com/sheypoor/log4k.\n\nUsage\n---\nAfter installation, you just need to call a one letter function like the following.\n```kotlin\n     d(\"This is my log\")\n```\nFor more advance logging experience you can describe the assumptions of a method with this library as follows.\n```kotlin\n   fun exampleMethod(foo: Boolean, bar: Boolean, ..., baz: Any?) {\n       assumeTrue(\"Message of AssertionError when this assumption is not satisfied\", foo)\n           ?.assumeFalse(\"The same as the above message\", bar)\n           ...\n           ?.assumeNotNull(\"An other message\", baz) {\n               // If all the above assumptions get satisfied then this block will be running.\n               // Else an Assertion log will be triggered on the proper Appenders.\n           }\n           // Add more assumptions for the next block\n           ?.assumeEquals(\"A message\", foo, bar) {\n               // This block will be running if all the above assumptions are true.\n           }\n   }\n```\nIn this way, a new developer in your team easily will understand the assumptions of the writer of the method. Also, if\neach of the assumptions is not satisfied in the runtime, you can log the AssertionError to the Crashlytics or other\nservices for another review of the method in the future.\n\nInstallation\n---\nSince version 2.0.0 this library is a Kotlin Multiplatform library, so depend on what kind of module you use it in, you can use different artifact IDs. Download it in gradle for \n\n * `Android` \n```groovy\nimplementation \"com.github.hadilq:log4k-android:${log4kVersion}\"\n```\n * `JVM`\n```groovy\nimplementation \"com.github.hadilq:log4k-jvm:${log4kVersion}\"\n```\n * `Javascript`\n```groovy\nimplementation \"com.github.hadilq:log4k-js:${log4kVersion}\"\n```\n * `Common`\n```groovy\nimplementation \"com.github.hadilq:log4k-metadata:${log4kVersion}\"\n```\nAnd finally in case of native module, for instance use\n```groovy\nimplementation \"com.github.hadilq:log4k-iosArm64:${log4kVersion}\"\n```\n\nBut be aware that here we annotated `object Log4k` with `kotlin.native.concurrent.ThreadLocal`, so you may need to apply the next step cofiguration on each thread that you want your logs not be ignored!\n\nSnapshots of the development version are available in [Sonatype's snapshots repository](https://oss.sonatype.org/content/repositories/snapshots).\n\nIn the next step, we need to implement the visitors(in visitor design pattern sense), so for instance in Android apps, inside the `onCreate` method of your `Application` class setup it like this\n```kotlin\nif (BuildConfig.DEBUG) {\n    Log4k.add(Level.Verbose, \".*\", AndroidAppender())\n    Log4k.add(Level.Verbose, \"com\\\\.log4k\\\\.sample\\\\..+\", JVMAppender())\n    Log4k.add(Level.Verbose, \".*\", JVMAppender(writer = PrintWriter(File(externalCacheDir, \"debug-log.txt\"))))\n} else {\n    Log4k.add(Level.Assert, \"com\\\\.log4k\\\\.sample\\\\..+\", JVMAppender(writer = PrintWriter(File(filesDir, \"log.txt\"))))\n}\n```\nAs you see, you can use any kind of appender to handle different kinds of logs. For instance, the `AndroidAppender`\nappender log as an ordinary Android `Log`. Or the `JVMAppender(writer = PrintWriter(File(externalCacheDir, \"debug-log.txt\")))`\nappender log into a file in external cache directory. And done!\n\nMore Usage\n---\n\nBy the way, after installation you can use other features of this library. For instance, you can use `com.log4k.LoggerConfig` annotation in Android and JVM modules like this\n```kotlin\n@LoggerConfig(tag = \"Here, if you need, can change the tag from the `qualifiedName` of `Example` class to any other `String`\")\nclass Example {\n  fun someMethod() {\n    d(\"Reached here!\") // Debug level\n\n    try {\n      ...\n    } catch(error: Throwbale) {\n      e(\"Message\", error) // Error level\n    }\n    ...\n    object : Comparable\u003cFloat\u003e {\n      override fun compareTo(other: Float): Int {\n        // This is an anonymous class so using `d(\"Message\")`\n        // would not capture `LoggerConfig` config. \n        // Instead we can use\n        \"Message\".i\u003cExample\u003e() // Info level\n      }\n    }\n    ...\n    SomeCustomeEventForTrackingInFirebase().(v())() // Verbose level\n  }\n}\n```\nwhere `LoggerConfig` defined as following.\n```kotlin\nannotation class LoggerConfig(\n  val enable: Boolean = true,\n  val tag: String = \"\",\n  val owners: Array\u003cString\u003e = [],\n)\n```\nso you can disable all logs of the `Example` class all together by setting `enable = false`. Or you can use this \nannotation to clarify who are the owners of this class.\n\nAs you find out in the above example there are different styles for logger methods, where I'm looking to find out which\none is working best for the users, so your feedback is welcome.\n\n\nContribution\n---\nJust create your branch from master branch, change it, write additional tests, satisfy all tests, create your pull\nrequest, thank you, you're awesome.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadilq%2Flog4k","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhadilq%2Flog4k","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadilq%2Flog4k/lists"}