{"id":13800983,"url":"https://github.com/jokade/slogging","last_synced_at":"2025-08-01T10:08:26.097Z","repository":{"id":20201870,"uuid":"23473265","full_name":"jokade/slogging","owner":"jokade","description":"A Typesafe-logging (and slf4j) compatible logging library based on macros for Scala/JVM, Scala.js, and Scala Native","archived":false,"fork":false,"pushed_at":"2020-09-17T15:39:59.000Z","size":116,"stargazers_count":51,"open_issues_count":13,"forks_count":11,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-11T11:31:27.931Z","etag":null,"topics":["log-statements","scala","scala-js","scala-native","slf4j"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jokade.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2014-08-29T18:17:29.000Z","updated_at":"2024-12-09T19:54:56.000Z","dependencies_parsed_at":"2022-09-03T19:10:54.465Z","dependency_job_id":null,"html_url":"https://github.com/jokade/slogging","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jokade%2Fslogging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jokade%2Fslogging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jokade%2Fslogging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jokade%2Fslogging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jokade","download_url":"https://codeload.github.com/jokade/slogging/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243823392,"owners_count":20353663,"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":["log-statements","scala","scala-js","scala-native","slf4j"],"created_at":"2024-08-04T00:01:18.255Z","updated_at":"2025-03-16T17:35:41.699Z","avatar_url":"https://github.com/jokade.png","language":"Scala","readme":"slogging\n========\n\u003c!-- [![Build Status](https://travis-ci.org/jokade/slogging.svg?branch=master)](https://travis-ci.org/jokade/slogging) --\u003e\n\nA simple logging library for Scala, [Scala.js](http://www.scala-js.org), and [Scala Native](http://www.scala-native.org/en/latest/). Slogging is compatible to the [scala-logging](https://github.com/typesafehub/scala-logging) (and slf4j) API, and uses macros to check if logging statements should be executed.\n\n**News:** Version 0.6.2 has been released ([release notes](https://github.com/jokade/slogging/wiki/Release-Notes))!\n\n#### Contents:\n* [Getting Started](#getting-started)\n  * [SBT Settings](#sbt-settings)\n  * [Logging and Configuration](#logging-and-configuration)\n  * [FilterLogger](#filterlogger)\n* [Backends](#backends)\n  * [PrintLogger](#printlogger)\n  * [Scala / JVM](#scala--jvm)\n    * [SLF4J](#slf4jloggerfactory)\n  * [Scala.js](#scalajs)\n    * [ConsoleLogger](#consoleloggerfactory)\n    * [Winston (Node.js)](#winstonloggerfactory)\n    * [Remote HTTP](#httploggerfactory)\n  * [Scala Native](#scala-native)\n    * [TerminalLogger](#terminallogger)\n    * [Syslog](#sysloglogger)\n    * [GLib](#gliblogger)\n\nGetting Started\n---------------\n\n### SBT Settings\nAdd one of the following lines to your `build.sbt` (depending on your target):\n\n**Scala/JVM** with logging to stdout:\n```scala\nlibraryDependencies += \"biz.enef\" %% \"slogging\" % \"0.6.2\"\n```\nwith slf4j:\n```scala\nlibraryDependencies ++= Seq(\n  \"biz.enef\" %% \"slogging-slf4j\" % \"0.6.2\",\n  \"org.slf4j\" % \"slf4j-simple\" % \"1.7.+\"  // or another slf4j implementation\n)\n```\n\n**Scala.js** with logging to console:\n```scala\nlibraryDependencies += \"biz.enef\" %%% \"slogging\" % \"0.6.2\"\n```\nwith [winston](https://www.npmjs.com/package/winston) (Node.js):\n```scala\nlibraryDependencies += \"biz.enef\" %%% \"slogging-winston\" % \"0.6.2\"\n```\nwith remote logging via HTTP POST:\n```scala\nlibraryDependencies += \"biz.enef\" %%% \"slogging-http\" % \"0.6.2\"\n```\n\n**Scala Native** with logging to stderr:\n```scala\nlibraryDependencies += \"biz.enef\" %%% \"slogging\" % \"0.6.2\"\n```\n\nwith logging to `syslogd`:\n```scala\nlibraryDependencies += \"biz.enef\" %%% \"slogging-syslog\" % \"0.6.2\"\n```\n\nwith logging to [GLib](https://developer.gnome.org/glib/stable/glib-Message-Logging.html):\n```scala\nlibraryDependencies += \"biz.enef\" %%% \"slogging-glib\" % \"0.6.2\"\n```\n\nslogging 0.6.2 is published for both Scala 2.12.x and Scala 2.13.x,\nScala.js 1.0, and Scala Native 0.3.9 / 0.4.0-M2.\n\n### Logging and Configuration\n#### Add logging statements\nMix one of two traits into your class/object to give it logging capability:\n* `StrictLogging`: the `logger` is initialized when the instance is created\n* `LazyLogging`: the `logger` is initialized on first access\n\nExample:\n```scala\nimport slogging.LazyLogging\n\nclass Foo extends LazyLogging {\n  def bar() {\n    // log an error message\n    logger.error(\"...\")\n    // log a warning\n    logger.warn(\"...\")\n    // log an info message\n    logger.info(\"...\")\n    // log a debug message\n    logger.debug(\"...\")\n    // log a tracing message\n    logger.trace(\"...\")\n    // log a parameterized message\n    logger.info(\"foo={}, bar={}\",1,true)\n  }\n}\n```\n\n#### Activate logging and set log level\nThe default logger is a `NullLogger` which simply ignores all logging statements. To activate logging output, assign a different logger factory to `LoggerConfig.factory`, or call the `apply()` method of the logger factory you want to use. Assign a new value to `LoggerConfig.level` to change the current log level. Example:\n```scala\nimport slogging._\n\nobject Main extends App {\n  // select logger backend, e.g. simple logging using println (supported by Scala/JVM, Scala.js, and Scala Native) \n  LoggerConfig.factory = PrintLoggerFactory()\n\n  // set log level, e.g. to DEBUG\n  LoggerConfig.level = LogLevel.DEBUG\n}\n```\n**Note**: Some backends (slf4j, winston) have their own log level management, which needs to be adjusted as well.\n\n#### Disable logging at compile time\nIt is possible to omit logging statements from the generated code altogether. To this end add the following flag to your `build.sbt`:\n```scala\nscalacOptions += \"-Xmacro-settings:slogging.disable\"\n```\n\n### FilterLogger\nSometimes it is useful to enable detailed logging (e.g. `LogLevel.TRACE`) only for some classes. This can be achieved using `FilterLoggerFactory` and setting `FilterLogger.filter` to a custom filter PartialFunction. The filter function receives a tuple `(LogLevel.Value,String)` that contains the log level and logger source name for every logging message; it must return the `UnderlyingLogger` instance to be used for this logging statement (and must be defined for every input, so make sure to provide a default case):\n```scala\nimport slogging._\n\nLoggerConfig.factory = FilterLoggerFactory()\nLoggerConfig.level = LogLevel.TRACE\n\nFilterLogger.filter = {\n  // use PrintLogger for all trace statements from sources starting with \"foo.bar\"\n  case (LogLevel.TRACE,source) if source.startsWith(\"foo.bar\") =\u003e PrintLogger\n  // ignore all other trace statements\n  case (LogLevel.TRACE,_) =\u003e NullLogger\n  // log all other levels\n  case _ =\u003e PrintLogger\n}\n```\n**Note:** The filter function is called _after_ the current value of `LoggerConfig.level` has been checked. Hence, even if you want to log TRACE statements for a specific source using `FilterLogger`, you need to set `FilterConfig.level = LogLevel.TRACE`. This also means that _all_ TRACE logging  statements in the code are executed, even if they are subsequently discarded by `NullLogger`, which may have a serious impact on performance.\n\nBackends\n--------\n### PrintLogger\nThis simple backend prints all messages to stderr (or stdout) and can be used with JVM, JS an Scala Native projects.\n\n**Usage:**\n```scala\n// build.sbt\nlibraryDependencies += \"biz.enef\" %% \"slogging\" % \"VERSION\"\n```\n```scala\nimport slogging._\n\n// activate PrintLogger\nLoggerConfig.factory = PrintLoggerFactory()\n\n// set this to activate timestamp logging\nPrintLogger.printTimestamp = true\n```\nYou can change the output stream to which messages are logged for each level, e.g.:\n```scala\n// use stderr for ERROR and WARN\nPrintLoggerFactory.errorStream = System.err\nPrintLoggerFactory.warnStream = System.err\n\n// use stdout for all other levels\nPrintLoggerFactory.infoStream = System.out\nPrintLoggerFactory.debugStream = System.out\nPrintLoggerFactory.traceStream = System.out\n```\n\nFurthermore, you may also change the format of logging message by providing a custom `MessageFormatter`:\n```scala\nobject CustomFormatter extends MessageFormater {\n  def formatMessage(level: MessageLevel, name: String, msg: String, cause: Option[Throwable]): String = {\n    /* ... */\n  }\n}\n\nPrintLoggerFactory.formatter = CustomFormatter\n```\n\n### Scala / JVM\nslogging supports the following logger backends on Scala (JVM):\n\n#### SLF4JLoggerFactory\nThis backend is just a wrapper around [slf4j](http://www.slf4j.org).\n\n**Usage:**\n```scala\n// build.sbt\nlibraryDependencies ++= Seq(\n  \"biz.enef\" %% \"slogging-slf4j\" % \"VERSION\",\n  \"org.slf4j\" % \"slf4j-simple\" % \"1.7.+\"  // or another slf4j implementation\n)\n```\n```scala\nimport slogging._\n\n// activate SLF4J backend\nLoggerConfig.factory = SLF4JFactory()\n```\n\n### Scala.js\nslogging support the following logger backends for Scala.js:\n\n#### ConsoleLoggerFactory\nSimilar to PrintLoggerFactory, but uses `console.log` instead of `println()`.\n\n**Usage:**\n```scala\n// build.sbt\nlibraryDependencies += \"biz.enef\" %%% \"slogging\" % \"VERSION\"\n```\n```scala\nimport slogging._\n\n// activate ConsoleLogger; no additional configuration required\nLoggerConfig.factory = ConsoleLoggerFactory()\n```\n\n#### WinstonLoggerFactory\nA wrapper around the [winston](https://www.npmjs.com/package/winston) logging library for Node.js.\n\n**Usage:**\n```scala\n// build.sbt\nlibraryDependencies += \"biz.enef\" %%% \"slogging-winston\" % \"VERSION\"\n\n// activate module support to load winston module\nscalaJSModuleKind := ModuleKind.CommonJSModule\n```\n\n```scala\nimport slogging._\n\n// use default winston logger\nLoggerConfig.factory = WinstonLoggerFactory()\n\n// use a custom winston logger\nimport WinstonLoggerFactory.{WinstonLogger, winston}\n\nLoggerConfig.factory = WinstonLoggerFactory(WinstonLogger(literal(\n  levels = js.Dictionary(\n    \"trace\" -\u003e 0,\n    \"debug\" -\u003e 1,\n    \"info\"  -\u003e 2,\n    \"warn\"  -\u003e 3,\n    \"error\" -\u003e 4\n  ),\n  transports = js.Array(\n    js.Dynamic.newInstance(winston.transports.Console)(literal(\n      level = \"debug\"\n    ))\n  )\n)))\n```\n\n#### HttpLoggerFactory\nThis backend sends log messages to a HTTP server via Ajax POST requests. Note that the same origin policy usually requires that the server to which the messages are sent must be the same server from which the javascript source was loaded.\n\n**Usage:**\n```scala\nimport slogging._\n\n// function that assembles the JSON object to be sent\n// (only required if you want to override the default formatter)\nval fmt: HttpLoggerFactory.MessageFormatter = (clientId,level,name,msg,cause) =\u003e js.Dynamic.literal(\n  id = clientId,\n  loggerName = name,\n  logMessage = msg\n)\n\n// configure \u0026 activate remote HTTP logging \nLoggerConfig.factory = \n  HttpLoggerFactory(\"/logging\", // target URL for log messages\n                    \"client1\",  // ID of the client that sends the messages (optional)\n                    fmt         // message formatting function (optional)\n                   ) \n```\n\n### Scala Native\nslogging supports the following logger backends on Scala Native:\n\n#### TerminalLogger\nLogs all messages to stderr using fprintf. Messages are enclosed in ANSI terminal control codes, which can be configured separately for every level.\n\n**Usage:**\n```scala\n// build.sbt\nlibraryDependencies += \"biz.enef\" %%% \"slogging\" % \"VERSION\"\n```\n\n```scala\n// use default TerminalLogger\n// (error messages are printed in red, warnings in yellow)\nLoggerConfig.factory = TerminalLoggerFactory()\n\n// print INFO messages in blue\nTerminalLoggerFactory.infoCode = TerminalControlCode.blue\n\n// change the message formatter\nTerminalLoggerFactory.formatter = ...\n```\n\n#### SyslogLogger\nThis backend uses the standard `syslog` facility.\n\n**Usage:**\n```scala\n// build.sbt\nlibraryDependencies += \"biz.enef\" %%% \"slogging-syslog\" % \"VERSION\"\n```\n\n```scala\nLoggerConfig.factory = SyslogLoggerFactory()\n```\n\n#### GLibLogger\nThis backend uses [GLib's `g_log()`](https://developer.gnome.org/glib/stable/glib-Message-Logging.html#g-log) to log messages,\ne.g. for use with [scalanative-gtk](https://github.com/jokade/scalanative-gtk).\n\n**Usage:**\n```scala\n// build.sbt\nlibraryDependencies += \"biz.enef\" %%% \"slogging-glib\" % \"VERSION\"\n\nnativeLinkingOptions += \"-lglib-2.0\" // you may need to change this depending on your glib installation\n```\n\n```scala\nLoggerConfig.factory = GLibLoggerFactory()\n\n// optionally change message formatter\nGLibLoggerFactory.formatter = ...\n```\n\n\nLicense\n-------\nThis code is open source software licensed under the [MIT License](http://opensource.org/licenses/MIT)\n","funding_links":[],"categories":["Logging"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjokade%2Fslogging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjokade%2Fslogging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjokade%2Fslogging/lists"}