@5app/logger
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -8,2 +8,5 @@ { | ||
], | ||
"env": { | ||
"mocha": true | ||
}, | ||
"rules": { | ||
@@ -16,2 +19,3 @@ "array-bracket-newline": [2, "consistent"], | ||
"dot-location": [2, "property"], | ||
"no-console": 0, | ||
"no-empty-function": 2, | ||
@@ -18,0 +22,0 @@ "no-magic-numbers": 0, |
@@ -0,1 +1,13 @@ | ||
# [2.0.0](https://github.com/5app/logger/compare/v1.1.0...v2.0.0) (2019-09-12) | ||
### Features | ||
* Drop winston and simpplify the logger ([fa86131](https://github.com/5app/logger/commit/fa86131)) | ||
### BREAKING CHANGES | ||
* The logger do not allow using winston's API anymore as we dropped winston | ||
# [1.1.0](https://github.com/5app/logger/compare/v1.0.2...v1.1.0) (2019-09-12) | ||
@@ -2,0 +14,0 @@ |
32
index.js
@@ -1,20 +0,20 @@ | ||
const getLogger = require('./getLogger'); | ||
const simple = require('./outputs/simple'); | ||
const json = require('./outputs/json'); | ||
const levels = require('./levels'); | ||
const priority = require('./priority'); | ||
const { | ||
LOGS_FORMAT, | ||
LOGS_LEVEL, | ||
TAG | ||
} = process.env; | ||
const {LOGS_FORMAT, LOGS_LEVEL} = process.env; | ||
const logger = getLogger({ | ||
simple: LOGS_FORMAT !== 'json', | ||
level: LOGS_LEVEL || 'debug', | ||
metadata: { | ||
tag: TAG | ||
} | ||
const logger = LOGS_FORMAT === 'json' ? json : simple; | ||
const minimumPriority = priority(LOGS_LEVEL || 'debug'); | ||
// eslint-disable-next-line no-empty-function | ||
const noLog = () => {}; | ||
const methods = {}; | ||
Object.keys(levels).forEach(level => { | ||
methods[level] = priority(level) >= minimumPriority ? (message, context) => logger(level, message, context) : noLog; | ||
}); | ||
module.exports = { | ||
logger, | ||
getLogger | ||
}; | ||
module.exports = methods; |
{ | ||
"name": "@5app/logger", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Logger used in 5app microservices", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"precommit-msg": "echo 'Running pre-commit checks... (skip using --no-verify)' && exit 0", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha" | ||
}, | ||
@@ -40,2 +40,3 @@ "pre-commit": [ | ||
"eslint-plugin-security": "^1.4.0", | ||
"mocha": "^6.2.0", | ||
"pre-commit": "^1.2.2", | ||
@@ -67,3 +68,3 @@ "semantic-release": "^15.13.24" | ||
"dependencies": { | ||
"winston": "^3.2.1" | ||
"chalk": "^2.4.2" | ||
}, | ||
@@ -70,0 +71,0 @@ "publishConfig": { |
@@ -10,6 +10,4 @@ # logger | ||
Then, generate a logger in your app and use it to log messages. | ||
```javascript | ||
const {logger} = require('@5app/logger'); | ||
const logger = require('@5app/logger'); | ||
@@ -24,77 +22,21 @@ logger.info('An email was sent', { | ||
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({ | ||
simple: process.env.NODE_ENV === 'development', | ||
metadata: { | ||
tag: process.env.TAG, // release tag, e.g. docker container tag | ||
}, | ||
}); | ||
logger.info('An email was sent', { | ||
email: 'customer@5app.com', | ||
template: 'template1', | ||
}); | ||
logger.error(new Error('Unknown playlist 123')); | ||
``` | ||
## Options | ||
The logger generator accepts the following options: | ||
The logger can optionally be customised using the following environment variables: | ||
### simple | ||
- `LOGS_FORMAT`: if set to `json`, the logger will log messages in json format instead of pretty messages (default behaviour). | ||
- `LOGS_LEVEL`: minimum logging level, by default it will be `debug`. Accepted values are `'debug'`, `'info'`, `'warn'`, and `'error'` | ||
- `TAG`: release tag (e.g. docker image tag) to be added to the log messages | ||
This boolean value specifies whether we should log pretty dev-friendly messages instead of JSON or not. | ||
By default, simple will be false. | ||
## Logging levels | ||
Example: | ||
```javascript | ||
const {getLogger} = require('@5app/logger'); | ||
Logging levels are (from lower to higher priority): `'debug'`, `'info'`, `'warn'`, and `'error'`. | ||
The logger provides the logging functions with the following signatures: `logger.<level>(message, objectOrError)` | ||
// This logger will log dev-friendly messages | ||
const logger = getLogger({ | ||
simple: true, | ||
}); | ||
Here is an example of how the logger can be used: | ||
``` | ||
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 | ||
Metadata is an object containing service metadata like the release tag or the A/B testing experiment we are running. | ||
This object will be added to each log entry when using the JSON mode. | ||
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({ | ||
metadata: { | ||
release: '1.2.3', | ||
experiment: 12345, | ||
}, | ||
}); | ||
logger.error('An error happened', new ApiError('The api call failed', 404)); // will log the message, the error message, the stack trace, and the statusCode error property | ||
logger.warn('Be warned', {a: 1, b: Date.now(), c: 'some string'}); | ||
logger.info('An event happened', {a: 1, b: Date.now(), c: 'some string'}); | ||
logger.debug('A minor operation', {a: 1, b: Date.now(), c: 'some string'}); | ||
``` | ||
### 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'); | ||
``` |
Sorry, the diff of this file is not supported yet
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
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
10197
12
121
0
9
41
2
+ Addedchalk@^2.4.2
+ Addedansi-styles@3.2.1(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
- Removedwinston@^3.2.1
- Removed@colors/colors@1.6.0(transitive)
- Removed@dabh/diagnostics@2.0.3(transitive)
- Removed@types/triple-beam@1.3.5(transitive)
- Removedasync@3.2.6(transitive)
- Removedcolor@3.2.1(transitive)
- Removedcolor-string@1.9.1(transitive)
- Removedcolorspace@1.1.4(transitive)
- Removedenabled@2.0.0(transitive)
- Removedfecha@4.2.3(transitive)
- Removedfn.name@1.1.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arrayish@0.3.2(transitive)
- Removedis-stream@2.0.1(transitive)
- Removedkuler@2.0.0(transitive)
- Removedlogform@2.7.0(transitive)
- Removedms@2.1.3(transitive)
- Removedone-time@1.0.0(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafe-stable-stringify@2.5.0(transitive)
- Removedsimple-swizzle@0.2.2(transitive)
- Removedstack-trace@0.0.10(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedtext-hex@1.0.0(transitive)
- Removedtriple-beam@1.4.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwinston@3.17.0(transitive)
- Removedwinston-transport@4.9.0(transitive)