Skip to content

Commit

Permalink
Merge pull request #26 from s3pweb/feature/v2
Browse files Browse the repository at this point in the history
Feature/v2
  • Loading branch information
Mualig authored May 17, 2023
2 parents 5b0f6cc + 2d0db1f commit 6b396f4
Show file tree
Hide file tree
Showing 9 changed files with 721 additions and 6,249 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.

31 changes: 8 additions & 23 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,14 +83,6 @@ class Logger {
console.error('Error on logstash stream', err)
})
})
} else if (config.logger.server.type === 'ovh') {
const OvhStream = require('./ovh-stream')

this.log.addStream({
level: config.logger.server.level,
type: 'raw',
stream: new OvhStream()
})
}
}
}
Expand All @@ -116,9 +107,3 @@ function convertConfigToNumber (value) {
}
return numberValue
}

const instanceLogger = new Logger()

Object.freeze(instanceLogger)

module.exports.logger = instanceLogger.get()
74 changes: 0 additions & 74 deletions lib/ovh-stream.js

This file was deleted.

Loading

0 comments on commit 6b396f4

Please sign in to comment.