Skip to content

Commit

Permalink
feat(logger)!: remove mandatory use of node-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mualig committed May 17, 2023
1 parent 087f315 commit c1455db
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 115 deletions.
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -47,30 +41,27 @@ 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

```bash
NODE_ENV=test node example/example.js
```

# Bonus :
# Bonus

To start a ELK stack on docker :

Expand Down
3 changes: 2 additions & 1 deletion config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 0 additions & 27 deletions config/localhost.json

This file was deleted.

27 changes: 0 additions & 27 deletions config/production.json

This file was deleted.

25 changes: 9 additions & 16 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
})
Expand All @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -116,9 +115,3 @@ function convertConfigToNumber (value) {
}
return numberValue
}

const instanceLogger = new Logger()

Object.freeze(instanceLogger)

module.exports.logger = instanceLogger.get()
13 changes: 1 addition & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
30 changes: 17 additions & 13 deletions test/console.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
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()
})

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()
})

0 comments on commit c1455db

Please sign in to comment.