From c1455db2a3b7ac364428c1313ca514be01dd50ec Mon Sep 17 00:00:00 2001 From: Guillaume GABOREL Date: Wed, 17 May 2023 11:10:38 +0200 Subject: [PATCH] feat(logger)!: remove mandatory use of node-config --- README.md | 27 +++++++-------------- config/custom-environment-variables.json | 3 ++- config/localhost.json | 27 --------------------- config/production.json | 27 --------------------- lib/logger.js | 25 +++++++------------- package-lock.json | 13 +--------- package.json | 1 - test/console.js | 30 ++++++++++++++---------- 8 files changed, 38 insertions(+), 115 deletions(-) delete mode 100644 config/localhost.json delete mode 100644 config/production.json diff --git a/README.md b/README.md index 2286c1c..e95e3b8 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,9 @@ This lib can send logs to the console, to a file or to a logstash instance. npm install @s3pweb/s3pweb-logger -# Config +# Configuration -Configurations are stored in configuration files within your application, and can be overridden and extended by -environment variables, command line parameters, or external sources. See : http://lorenwest.github.io/node-config/ - -1. Create a config folder - -2. Create a file(s) with the name of your environment(s) like test.json - -3. Paste this configuration template : +The constructor expect a config object following this format: ```json { @@ -30,7 +23,8 @@ environment variables, command line parameters, or external sources. See : http: "file": { "enable": true, "level": "info", - "dir": "./logs" + "dir": "./logs", + "addHostnameToPath": true }, "server": { "enable": true, @@ -47,22 +41,19 @@ environment variables, command line parameters, or external sources. See : http: } ``` -4. Adapt values with your expectations - -# Example : +# Example ```js -const log = require('@s3pweb/s3pweb-logger').logger -const { logger } = require('@s3pweb/s3pweb-logger') +const Logger = require('@s3pweb/s3pweb-logger') +const log = new Logger(config).get() const child = log.child({ child: 'childName' }) log.info('one message from log') -logger.info('one message from logger') child.info('one message from child') ``` -# Run example : +# Tests Set the name of your environment with NODE_ENV=xxxx before node @@ -70,7 +61,7 @@ Set the name of your environment with NODE_ENV=xxxx before node NODE_ENV=test node example/example.js ``` -# Bonus : +# Bonus To start a ELK stack on docker : diff --git a/config/custom-environment-variables.json b/config/custom-environment-variables.json index e6df174..94f6c65 100644 --- a/config/custom-environment-variables.json +++ b/config/custom-environment-variables.json @@ -9,7 +9,8 @@ "file": { "enable": "LOGGER_FILE_ENABLE", "level": "LOGGER_FILE_LEVEL", - "dir": "LOGGER_FILE_DIR" + "dir": "LOGGER_FILE_DIR", + "addHostnameToPath": "LOGGER_FILE_ADD_HOSTNAME" }, "server": { "enable": "LOGGER_SERVER_ENABLE", diff --git a/config/localhost.json b/config/localhost.json deleted file mode 100644 index 2274f1b..0000000 --- a/config/localhost.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "name": "s3pweb-logger", - "logger": { - "source": false, - "console": { - "enable": true, - "level": "debug" - }, - "file": { - "enable": true, - "level": "info", - "dir": "./logs", - "addHostnameToPath": false - }, - "server": { - "enable": true, - "level": "trace", - "url": "0.0.0.0", - "port": "9998", - "type": "elk" - }, - "ringBuffer": { - "enable": true, - "size": 5 - } - } -} diff --git a/config/production.json b/config/production.json deleted file mode 100644 index bf02a93..0000000 --- a/config/production.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "logger": { - "source": false, - "console": { - "enable": true, - "level": "debug" - }, - "file": { - "enable": false, - "level": "info", - "dir": "./logs", - "addHostnameToPath": false - }, - "server": { - "enable": true, - "level": "trace", - "url": "x.x.x.x", - "port": "9998", - "type": "elk", - "X-OVH-TOKEN": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" - }, - "ringBuffer": { - "enable": true, - "size": 5 - } - } -} diff --git a/lib/logger.js b/lib/logger.js index c146df6..2a68256 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,38 +1,37 @@ 'use strict' -const config = require('config') const bunyan = require('bunyan') const RotatingFileStream = require('bunyan-rotating-file-stream') const mkdirp = require('mkdirp') const os = require('os') const errSerializer = require('./errSerializer').errSerializer -class Logger { - constructor () { +module.exports = class Logger { + constructor (config) { // Create the base bunyan logger this.log = bunyan.createLogger({ name: 's3pweb-logger', application: config.name, // WARNING: Determining the call source info is slow. Never use this option in production. - src: convertConfigToBoolean(config.logger.source), + src: convertConfigToBoolean(config?.logger?.source), serializers: { err: errSerializer }, streams: [] }) - if (config.logger && config.logger.console && convertConfigToBoolean(config.logger.console.enable)) { + if (convertConfigToBoolean(config?.logger?.console?.enable)) { this.log.addStream({ level: config.logger.console.level, stream: process.stdout }) } - if (config.logger && config.logger.file && convertConfigToBoolean(config.logger.file.enable)) { + if (convertConfigToBoolean(config?.logger?.file?.enable)) { // Set up the log directory let logsDirectory if (config.logger.file.dir) { - if (config.logger.file.addHostnameToPath) { + if (convertConfigToBoolean(config.logger.file.addHostnameToPath)) { logsDirectory = `${config.logger.file.dir}/${os.hostname()}` } else { logsDirectory = config.logger.file.dir @@ -57,7 +56,7 @@ class Logger { }) } - if (config.logger && config.logger.ringBuffer && convertConfigToBoolean(config.logger.ringBuffer.enable)) { + if (convertConfigToBoolean(config?.logger?.ringBuffer?.enable)) { this.ringbuffer = new bunyan.RingBuffer({ limit: convertConfigToNumber(config.logger.ringBuffer.size) }) @@ -69,7 +68,7 @@ class Logger { }) } - if (config.logger && config.logger.server && convertConfigToBoolean(config.logger.server.enable)) { + if (convertConfigToBoolean(config?.logger?.server?.enable)) { if (config.logger.server.type === 'elk') { this.log.addStream({ level: config.logger.server.level, @@ -84,7 +83,7 @@ class Logger { console.error('Error on logstash stream', err) }) }) - } else if (config.logger.server.type === 'ovh') { + } else if (config?.logger?.server?.type === 'ovh') { const OvhStream = require('./ovh-stream') this.log.addStream({ @@ -116,9 +115,3 @@ function convertConfigToNumber (value) { } return numberValue } - -const instanceLogger = new Logger() - -Object.freeze(instanceLogger) - -module.exports.logger = instanceLogger.get() diff --git a/package-lock.json b/package-lock.json index 739636b..5e69ac3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "bunyan": "^1.8.15", "bunyan-rotating-file-stream": "^2.0.3", "CBuffer": "^2.2.0", - "config": "^3.3.9", "gelf-pro": "^1.3.10", "mkdirp": "^3.0.1" }, @@ -1664,17 +1663,6 @@ "node": ">=10.18.0 <11 || >=12.14.0 <13 || >=14" } }, - "node_modules/config": { - "version": "3.3.9", - "resolved": "https://registry.npmjs.org/config/-/config-3.3.9.tgz", - "integrity": "sha512-G17nfe+cY7kR0wVpc49NCYvNtelm/pPy8czHoFkAgtV1lkmcp7DHtWCdDu+C9Z7gb2WVqa9Tm3uF9aKaPbCfhg==", - "dependencies": { - "json5": "^2.2.3" - }, - "engines": { - "node": ">= 10.0.0" - } - }, "node_modules/conventional-changelog": { "version": "3.1.25", "resolved": "https://registry.npmjs.org/conventional-changelog/-/conventional-changelog-3.1.25.tgz", @@ -4588,6 +4576,7 @@ "version": "2.2.3", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", + "dev": true, "bin": { "json5": "lib/cli.js" }, diff --git a/package.json b/package.json index 0124ac3..1aaf3dd 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "bunyan": "^1.8.15", "bunyan-rotating-file-stream": "^2.0.3", "CBuffer": "^2.2.0", - "config": "^3.3.9", "gelf-pro": "^1.3.10", "mkdirp": "^3.0.1" }, diff --git a/test/console.js b/test/console.js index 31f9790..958635e 100644 --- a/test/console.js +++ b/test/console.js @@ -1,13 +1,17 @@ const test = require('ava') -const log = require('..').logger +const Logger = require('..') + +const config = require('../config/test.json') + +const log = new Logger(config).get() test('test basic', (t) => { - log.trace('toto') - log.debug('toto') - log.info('toto') - log.warn('toto') - log.error('toto') - log.fatal('toto') + log.trace('trace') + log.debug('debug') + log.info('info') + log.warn('warn') + log.error('error') + log.fatal('fatal') t.pass() }) @@ -15,12 +19,12 @@ test('test basic', (t) => { test('test child', (t) => { const childLog = log.child({ child: 'childLog' }) - childLog.trace('msg child') - childLog.debug('msg child') - childLog.info('msg child') - childLog.warn('msg child') - childLog.error('msg child') - childLog.fatal('msg child') + childLog.trace('trace child') + childLog.debug('debug child') + childLog.info('info child') + childLog.warn('warn child') + childLog.error('error child') + childLog.fatal('fatal child') t.pass() })