Skip to content

Commit d24a39d

Browse files
committed
feat: Create basic logger generator
Create a simple, winston-based logger generator.
1 parent 1eed17e commit d24a39d

7 files changed

Lines changed: 7461 additions & 1 deletion

File tree

.circleci/config.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
defaults: &defaults
2+
working_directory: ~/repo
3+
docker:
4+
- image: circleci/node:8.4.0
5+
6+
version: 2
7+
jobs:
8+
checkout_code:
9+
<<: *defaults
10+
steps:
11+
- checkout
12+
- persist_to_workspace:
13+
root: ~/repo
14+
paths:
15+
- .
16+
17+
test:
18+
<<: *defaults
19+
steps:
20+
- attach_workspace:
21+
at: ~/repo
22+
- restore_cache:
23+
key: dependency-cache-{{ checksum "package.json" }}
24+
- run:
25+
name: install dependencies with npm
26+
command: npm install
27+
- save_cache:
28+
key: dependency-cache-{{ checksum "package.json" }}
29+
paths:
30+
- ./node_modules
31+
- run:
32+
name: Lint
33+
command: npm run lint
34+
- persist_to_workspace:
35+
root: ~/repo
36+
paths:
37+
- node_modules
38+
39+
release:
40+
<<: *defaults
41+
steps:
42+
- attach_workspace:
43+
at: ~/repo
44+
- checkout
45+
- run: npm run semantic-release
46+
47+
workflows:
48+
version: 2
49+
build:
50+
jobs:
51+
- checkout_code
52+
- test:
53+
requires:
54+
- checkout_code
55+
- release:
56+
context: org-global
57+
requires:
58+
- test

.eslintrc.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"root": true,
3+
"extends": "5app",
4+
"plugins": [
5+
"promise",
6+
"security"
7+
],
8+
"rules": {
9+
"array-bracket-newline": [2, "consistent"],
10+
"array-element-newline": [2, "consistent"],
11+
"arrow-body-style": [2, "as-needed"],
12+
"capitalized-comments": 2,
13+
"comma-dangle": 2,
14+
"dot-location": [2, "property"],
15+
"no-empty-function": 2,
16+
"no-magic-numbers": 0,
17+
"no-tabs": [2, {"allowIndentationTabs": true}],
18+
"multiline-comment-style": [2, "starred-block"],
19+
"padded-blocks": 2,
20+
"require-atomic-updates": 0,
21+
"require-await": 0,
22+
"spaced-comment": [2, "always"],
23+
"strict": 2,
24+
"template-curly-spacing": 2,
25+
"newline-per-chained-call": 2,
26+
"prefer-promise-reject-errors": 2,
27+
"promise/prefer-await-to-then": 2,
28+
"security/detect-unsafe-regex": 2
29+
}
30+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 5app
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,66 @@
11
# logger
2-
Simple console logger that outputs json in prod and pretty messages on dev
2+
3+
> Simple console logger that outputs json in prod and pretty messages on dev
4+
5+
## Usage
6+
```sh
7+
npm install --save @5app/logger
8+
```
9+
10+
Then, generate a logger in your app and use it to log messages.
11+
12+
```javascript
13+
const getLogger = require('@5app/logger');
14+
15+
const logger = getLogger({
16+
simple: process.env.NODE_ENV === 'development',
17+
metadata: {
18+
tag: process.env.TAG, // release tag, e.g. docker container tag
19+
},
20+
});
21+
22+
logger.info('An email was sent', {
23+
email: 'customer@5app.com',
24+
template: 'template1',
25+
});
26+
27+
logger.error(new Error('Unknown playlist 123'));
28+
```
29+
30+
## Options
31+
32+
The logger generator accepts the following options:
33+
34+
### simple
35+
36+
This boolean value specifies whether we should log pretty dev-friendly messages instead of JSON or not.
37+
By default, simple will be false.
38+
39+
Example:
40+
```javascript
41+
const getLogger = require('@5app/logger');
42+
43+
// This logger will log dev-friendly messages
44+
const logger = getLogger({
45+
simple: true,
46+
});
47+
```
48+
49+
### metadata
50+
51+
Metadata is an object containing service metadata like the release tag or the A/B testing experiment we are running.
52+
This object will be added to each log entry when using the JSON mode.
53+
54+
Example:
55+
```javascript
56+
const getLogger = require('@5app/logger');
57+
58+
// This logger will add details about the current release and A/B experiment to every log line
59+
const getLogger = require('@5app/logger');
60+
const logger = getLogger({
61+
metadata: {
62+
release: '1.2.3',
63+
experiment: 12345,
64+
},
65+
});
66+
```

index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const winston = require('winston');
2+
3+
const LEVEL = Symbol.for('level');
4+
const {format} = winston;
5+
const sqlFormat = {
6+
level: winston.config.npm.levels.debug,
7+
color: 'grey'
8+
};
9+
10+
winston.addColors({sql: sqlFormat.color});
11+
12+
const colorizer = format.colorize();
13+
const sqlFormatter = format((info, opts) => {
14+
15+
const level = info[LEVEL];
16+
17+
if (opts.colorize && level === 'sql') {
18+
19+
info.message = colorizer.colorize(level, `${info.level}: ${info.message}\n`);
20+
21+
}
22+
23+
return info;
24+
25+
});
26+
27+
function getFormatter({simple}) {
28+
29+
if (simple) {
30+
31+
return format.combine(format.splat(), format.colorize(), sqlFormatter({colorize: true}), format.simple());
32+
33+
}
34+
35+
return format.combine(format.splat(), format.timestamp(), format.json());
36+
37+
}
38+
39+
function getLogger({simple, metadata}) {
40+
41+
return winston.createLogger({
42+
level: simple ? 'debug' : 'info',
43+
levels: {
44+
...winston.config.npm.levels,
45+
sql: sqlFormat.level
46+
},
47+
format: getFormatter({simple}),
48+
defaultMeta: metadata,
49+
transports: [new winston.transports.Console()]
50+
});
51+
52+
}
53+
54+
module.exports = getLogger;

0 commit comments

Comments
 (0)