{"id":22246194,"url":"https://github.com/jpbaking/slf4n-logging","last_synced_at":"2025-03-25T11:23:54.403Z","repository":{"id":57363015,"uuid":"136650949","full_name":"jpbaking/slf4n-logging","owner":"jpbaking","description":"Simple Logging Facade for NodeJS","archived":false,"fork":false,"pushed_at":"2018-06-22T18:53:29.000Z","size":235,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T10:29:18.506Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/jpbaking.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":"2018-06-08T18:03:20.000Z","updated_at":"2018-06-17T17:32:42.000Z","dependencies_parsed_at":"2022-09-08T12:31:04.029Z","dependency_job_id":null,"html_url":"https://github.com/jpbaking/slf4n-logging","commit_stats":null,"previous_names":["jpbaking/slf4j-like-logging"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Fslf4n-logging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Fslf4n-logging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Fslf4n-logging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Fslf4n-logging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpbaking","download_url":"https://codeload.github.com/jpbaking/slf4n-logging/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245450758,"owners_count":20617412,"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":[],"created_at":"2024-12-03T05:26:34.292Z","updated_at":"2025-03-25T11:23:54.370Z","avatar_url":"https://github.com/jpbaking.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# slf4n-logging\n\n## Simple Logging Facade for NodeJS\n\nEmulating Simple Logging Facade for Java (SLF4J)\n\n![ss](./.readme/ss.png)\n\n## This shiz is asynchronous!\n\nYes, logger functions' implementation for writing to streams won't be a blocker.\n\nEvery log method returns a [bluebird `Promise`](https://www.npmjs.com/package/bluebird) -- for those that want to \"wait\" for logs to be written before proceding to next lines.\n\n_[slf4n-logging](https://github.com/jpbaking/slf4n-logging) by [jpbaking](https://github.com/jpbaking)_\n\n## How to use?\n\n### Basic Usage\n\nSimply `require('slf4n-logging')` from your \"main\" (entrypoint) `.js` then all of other modules would get the global `LoggerFactory`.\n\nSample File #1: `app.js`\n```javascript\nrequire('slf4n-logging');\n```\n\nSample File #2: `api/controller/health.js`\n```javascript\nconst log = LoggerFactory.getLogger('app:health');\n// ...\nlog.debug('some debug line %s', aStringVariable);\nlog.debug('some debug line %o', anObjectOrArray);\n// ...\nlog.error('some error line', error);\n```\n\n#### Formatting in `log.{level}([data][, ...args])`\n\nAs shown in the example above, syntax for log-level functions exactly the same as [`require('util').format(format[, ...args])`](https://nodejs.org/api/util.html#util_util_format_format_args) and [`console.log([data][, ...args])`](https://nodejs.org/api/console.html#console_console_log_data_args).\n\n### Safe Usage\n\nSome do not like adding anything to `global.*`. If you're one of those, you may use `require('slf4n-logging/safe')` instead:\n\nSample File: `api/helpers/UltimateHelper.js`\n```javascript\nconst log = require('slf4n-logging/safe')('app:ultimate');\n// ...\nlog.info('some info line');\n// ...\nlog.warn('some warn line', error);\n```\n\n### \"Manual\" Usage\n\nSample `loggerConfig` object:\n```javascript\n// example \nconst loggerConfig = {\n  layout: '[:level] :timestamp :logger - :message:error',\n  errorIndenter: '    \u003e ',\n  level: 'trace',\n  colors: {\n    enabled: 'true',\n    fatal: 'magenta',\n    error: 'red',\n    warn: 'yellow',\n    info: 'white',\n    debug: 'cyan',\n    trace: 'green'\n  },\n  stream: 'stderr',\n  terminateOnFail: 'true'\n}\n```\n\nSample `global.LoggerFactory` _(unsafe)_:\n```javascript\n// logger factory\n// unsafe usage (`global.LoggerFactory` is set)\nrequire('slf4n-logging/json')(loggerConfig, false);\n// default is `false` for `safe` (second argument)\nrequire('slf4n-logging/json')(loggerConfig);\n\n// actual logger\nconst log = LoggerFactory.getLogger('app:something');\n// ...\nlog.debug('some debug line');\n// ...\nlog.error('some error line', error);\n```\n\nSample **safe** usage _(returns a `LoggerFactory`; same one that would've been set to `global.*`)_:\n```javascript\n// logger factory\nconst LoggerFactory = require('slf4n-logging/json')(loggerConfig, true);\n\n// actual logger\nconst log = LoggerFactory.getLogger('app:something');\n// ...\nlog.trace('some trace line');\n// ...\nlog.fatal('some fatal line', error);\n```\n\n## The `log.{level}([data][, ...args])` functions\n\nAs described earlier, just use `log.{level}([data][, ...args])` functions very much like how you'd use [`console.log([data][, ...args])`](https://nodejs.org/api/console.html#console_console_log_data_args).\n\n```javascript\nconst log = LoggerFactory.getLogger('app:something');\n// ...\ntry {\n  log.trace('something trace %s', aString);\n  log.debug('something debug %o', anObject);\n  log.info('something info');\n  log.warn('something warn %d', aNumber);\n  // ...\n} catch (error) {\n  log.fatal('something fatal', error);\n  log.error('something error', error);\n}\n```\n\nHere's a list of available `log.{level}([data][, ...args])` functions:\n\n- **`log.fatal([data][, ...args])`** - log something at fatal level\n- **`log.error([data][, ...args])`** - log something at error level\n- **`log.warn([data][, ...args])`** - log something at warn level\n- **`log.info([data][, ...args])`** - log something at info level\n- **`log.debug([data][, ...args])`** - log something at debug level\n- **`log.trace([data][, ...args])`** - log something at trace level\n\n## The `log.is{level}Enabled()` function\n\nIntended string/s to log are expensive to generate outside of [util.format()](https://nodejs.org/api/util.html#util_util_format_format_args)? Check if level can be logged in the first place!!!\n\n```javascript\nconst log = LoggerFactory.getLogger('app:something');\n// ...\nif (log.isTraceEnabled()) {\n  log.trace('Stats:\\n%s', someHeavyStatistics.snapshot().prettyPrint());\n}\n```\n\nIn the example above, `#snapshot()` and/or `#prettyPrint()` is expensive to execute; hence, checking if the log level is enabled **first**.\n\n- **`#isFatalEnabled()`** - check if fatal is enabled\n- **`#isErrorEnabled()`** - check if error is enabled\n- **`#isWarnEnabled()`** - check if warn is enabled\n- **`#isInfoEnabled()`** - check if info is enabled\n- **`#isDebugEnabled()`** - check if debug is enabled\n- **`#isTraceEnabled()`** - check if trace is enabled\n\n## Configuration\n\n### Environment Variables\n\nHere are the environment variables that can be set to configure `slf4n-logging`:\n\n#### `LOG_LAYOUT`\n\n\u003e **\"Manual\" Usage** JSON Path: `\u003croot\u003e.layout`\n\nThe layout / pattern for every log line written.\n\n\u003e **`:timestamp [:level] :logger@:hostname (:pid) - :message:error`**\n\nPlaceholders:\n- **`:timestamp`** - is replaced by ISO8601 date string _(ie.: `2018-06-17T10:38:19.336Z`)_\n- **`:level`** - the level or verbosity of logs allowed to be written\n- **`:logger`** - the `loggerName`\n- **`:hostname`** - the machine hostname from where app is running\n- **`:pid`** - the process ID (PID) of running app\n- **`:message`** - the message to be logged\n- **`:error`** - to show/print error and/or `error.stack`\n- **`:[n]`** - new-line\n\nColor placeholders:\n\n| FORMATS          | FOREGROUND       | BACKGROUND       |\n| ---------------- | ---------------- | ---------------- |\n| `:c[reset]`      | `:c[black]`      | `:c[bg.black]`   |\n| `:c[bold]`       | `:c[red]`        | `:c[bg.red]`     |\n| `:c[dim]`        | `:c[green]`      | `:c[bg.green]`   |\n| `:c[underscore]` | `:c[yellow]`     | `:c[bg.yellow]`  |\n| `:c[blink]`      | `:c[blue]`       | `:c[bg.blue]`    |\n| `:c[reverse]`    | `:c[magenta]`    | `:c[bg.magenta]` |\n| `:c[hidden]`     | `:c[cyan]`       | `:c[bg.cyan]`    |\n|                  | `:c[white]`      | `:c[bg.white]`   |\n\nHow to color? Here's a sample:\n\n\u003e **`:c[bold]:c[level][:level]:c[reset] :logger - :message:c[level]:error`**\n\nIt'll look like:\n\n![ss_coloring.png](./.readme/ss_coloring.png)\n\n#### `LOG_LEVEL`\n\n\u003e **\"Manual\" Usage** JSON Path: `\u003croot\u003e.level`\n\nThe \"max\" level or verbosity of logs allowed to be written. Better explained by the list of accepted values below:\n\n- **`OFF`** - nothing will be printed\n- **`FATAL`** - only level `FATAL` will be printed\n- **`ERROR`** - levels `ERROR`, `FATAL`, will be printed\n- **`WARN`** - levels `WARN`, `ERROR`, `FATAL`, will be printed\n- **`INFO`** - levels `INFO`, `WARN`, `ERROR`, `FATAL`, will be printed\n- **`DEBUG`** - levels `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, will be printed\n- **`TRACE`** - levels `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, will be printed\n\nDefault is `INFO` if left unset.\n\n#### `LOG_COLORS_ENABLED`\n\n\u003e **\"Manual\" Usage** JSON Path: `\u003croot\u003e.colors.enabled`\n\nTo enable/disable colors in logging. Note that setting this to `true` will **NOT** override printing colors over non-TTY terminals/consoles/streams _(if stream is **NOT** TTY, colors won't be printed)_.\n\n- **`true`** - colors (`:c[*]`) will be printed\n- **`false`** - no color will be printed\n\nDefault is `true` if left unset.\n\n#### `LOG_COLORS_{level}`\n\n\u003e **\"Manual\" Usage** JSON Path: `\u003croot\u003e.colors.{level_in_lowercase}`\n\u003e  - `\u003croot\u003e.colors.fatal`\n\u003e  - `\u003croot\u003e.colors.error`\n\u003e  - `\u003croot\u003e.colors.warn`\n\u003e  - `\u003croot\u003e.colors.info`\n\u003e  - `\u003croot\u003e.colors.debug`\n\u003e  - `\u003croot\u003e.colors.trace`\n\n- **`LOG_COLORS_FATAL`** - color to be used for `FATAL` level logs _(default: magenta)_\n- **`LOG_COLORS_ERROR`** - color to be used for `ERROR` level logs _(default: red)_\n- **`LOG_COLORS_WARN`** - color to be used for `WARN` level logs _(default: yellow)_\n- **`LOG_COLORS_INFO`** - color to be used for `INFO` level logs _(default: white)_\n- **`LOG_COLORS_DEBUG`** - color to be used for `DEBUG` level logs _(default: green)_\n- **`LOG_COLORS_TRACE`** - color to be used for `TRACE` level logs _(default: cyan)_\n\nHere are the valid colors:\n\n| FORMATS    | FOREGROUND | BACKGROUND |\n| ---------- | ---------- | ---------- |\n| reset      | black      | bg.black   |\n| bold       | red        | bg.red     |\n| dim        | green      | bg.green   |\n| underscore | yellow     | bg.yellow  |\n| blink      | blue       | bg.blue    |\n| reverse    | magenta    | bg.magenta |\n| hidden     | cyan       | bg.cyan    |\n|            | white      | bg.white   |\n\n#### `LOG_ERROR_INDENTER`\n\n\u003e **\"Manual\" Usage** JSON Path: `\u003croot\u003e.errorIndenter`\n\nUsed to indent error stack. \"`    | `\" _(the default; four spaces, a pipe, then a space)_ would appear like:\n\n![](./.readme/ss_indenter.png)\n\nNot really necessary, sure there's overhead, but does look nice `:P` _(improves readability for some)_\n\n#### `LOG_STREAM`\n\n\u003e **\"Manual\" Usage** JSON Path: `\u003croot\u003e.stream`\n\nDictates which stream (`stdout` and/or `stderr`) to route log writing. \n\n| LOG_STREAM     | \"out\"            | \"err\"            |\n| -------------- | ---------------- | ---------------- |\n| **`DEFAULT`**  | `process.stdout` | `process.stdout` |\n| **`STDOUT`**   | `process.stdout` | `process.stdout` |\n| **`STDERR`**   | `process.stderr` | `process.stderr` |\n| **`STANDARD`** | `process.stdout` | `process.stderr` |\n\n\u003e **`FATAL`** and **`ERROR`** are logged to \"err\" streams.\n\u003e \n\u003e **`WARN`**, **`INFO`**, **`DEBUG`**, and **`TRACE`**, are logged to \"out\" streams.\n\n#### `LOG_TERMINATE_ON_FAIL`\n\n\u003e **\"Manual\" Usage** JSON Path: `\u003croot\u003e.terminateOnFail`\n\nBasically, whether or not to terminate running app if writing logs into stream fails.\n\n- **`true`** - duh?\n- **`false`** - duh?\n\nDefault is `true` if left unset.\n\n## License\n\nMIT License\n\nCopyright (c) 2018 Joseph Baking\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpbaking%2Fslf4n-logging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpbaking%2Fslf4n-logging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpbaking%2Fslf4n-logging/lists"}