@5app/logger
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -0,1 +1,8 @@ | ||
# [1.1.0](https://github.com/5app/logger/compare/v1.0.2...v1.1.0) (2019-09-12) | ||
### Features | ||
* Provide a logger instance by default ([6a932d6](https://github.com/5app/logger/commit/6a932d6)) | ||
## [1.0.2](https://github.com/5app/logger/compare/v1.0.1...v1.0.2) (2019-09-12) | ||
@@ -2,0 +9,0 @@ |
54
index.js
@@ -1,44 +0,20 @@ | ||
const winston = require('winston'); | ||
const getLogger = require('./getLogger'); | ||
const LEVEL = Symbol.for('level'); | ||
const {format} = winston; | ||
const sqlFormat = { | ||
level: winston.config.npm.levels.debug, | ||
color: 'grey' | ||
}; | ||
const { | ||
LOGS_FORMAT, | ||
LOGS_LEVEL, | ||
TAG | ||
} = process.env; | ||
winston.addColors({sql: sqlFormat.color}); | ||
const colorizer = format.colorize(); | ||
const sqlFormatter = format((info, opts) => { | ||
const level = info[LEVEL]; | ||
if (opts.colorize && level === 'sql') { | ||
info.message = colorizer.colorize(level, `${info.message}\n`); | ||
const logger = getLogger({ | ||
simple: LOGS_FORMAT !== 'json', | ||
level: LOGS_LEVEL || 'debug', | ||
metadata: { | ||
tag: TAG | ||
} | ||
return info; | ||
}); | ||
function getFormatter({simple}) { | ||
if (simple) { | ||
return format.combine(format.splat(), format.colorize(), sqlFormatter({colorize: true}), format.simple()); | ||
} | ||
return format.combine(format.splat(), format.timestamp(), format.json()); | ||
} | ||
function getLogger({simple, metadata}) { | ||
return winston.createLogger({ | ||
level: simple ? 'debug' : 'info', | ||
levels: { | ||
...winston.config.npm.levels, | ||
sql: sqlFormat.level | ||
}, | ||
format: getFormatter({simple}), | ||
defaultMeta: simple ? {} : metadata, | ||
transports: [new winston.transports.Console()] | ||
}); | ||
} | ||
module.exports = getLogger; | ||
module.exports = { | ||
logger, | ||
getLogger | ||
}; |
{ | ||
"name": "@5app/logger", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Logger used in 5app microservices", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,4 +13,17 @@ # logger | ||
```javascript | ||
const getLogger = require('@5app/logger'); | ||
const {logger} = require('@5app/logger'); | ||
logger.info('An email was sent', { | ||
email: 'customer@5app.com', | ||
template: 'template1', | ||
}); | ||
logger.error(new Error('Unknown playlist 123')); | ||
``` | ||
Alternatively, you can create a new instance of logger where you can specify the metadata, logging level, and whether you want simple logs or not: | ||
```javascript | ||
const {getLogger} = require('@5app/logger'); | ||
const logger = getLogger({ | ||
@@ -42,3 +55,3 @@ simple: process.env.NODE_ENV === 'development', | ||
```javascript | ||
const getLogger = require('@5app/logger'); | ||
const {getLogger} = require('@5app/logger'); | ||
@@ -51,2 +64,4 @@ // This logger will log dev-friendly messages | ||
The default logger uses the environment variable `LOGS_FORMAT` to determine if the logs are going to be generated in json (`json`) or simple console logs (any other value other than `json`). | ||
### metadata | ||
@@ -59,6 +74,5 @@ | ||
```javascript | ||
const getLogger = require('@5app/logger'); | ||
const {getLogger} = require('@5app/logger'); | ||
// This logger will add details about the current release and A/B experiment to every log line | ||
const getLogger = require('@5app/logger'); | ||
const logger = getLogger({ | ||
@@ -71,1 +85,19 @@ metadata: { | ||
``` | ||
### level | ||
You can specify a minimum logging level using the `level` parameter or using the `LOGS_LEVEL` environment variable. | ||
By default, the logging level will be [debug (npm)](https://github.com/winstonjs/winston#logging-levels). | ||
Example: | ||
```javascript | ||
const {getLogger} = require('@5app/logger'); | ||
// This logger will add details about the current release and A/B experiment to every log line | ||
const logger = getLogger({ | ||
level: 'warn', | ||
}); | ||
logger.info('this message will not be logged'); | ||
logger.warn('you will see this message'); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9354
8
79
99
1