Skip to content

Commit 5a67d87

Browse files
committed
feat: initial commit of open sourced code
0 parents  commit 5a67d87

16 files changed

+19525
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Continuous Integration
2+
3+
on: pull_request
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
timeout-minutes: 30
9+
strategy:
10+
matrix:
11+
node: [ 16, 18 ]
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-node@v3
15+
with:
16+
node-version: ${{ matrix.node }}
17+
- run: npm version
18+
- run: npm ci
19+
- run: npm run build
20+
21+
unit-test:
22+
needs: [build]
23+
runs-on: ubuntu-latest
24+
timeout-minutes: 30
25+
strategy:
26+
matrix:
27+
node: [ 16, 18 ]
28+
steps:
29+
- uses: actions/checkout@v3
30+
- uses: actions/setup-node@v3
31+
with:
32+
node-version: ${{ matrix.node }}
33+
- run: npm ci
34+
- run: npm run lint
35+
- run: npm run test

.github/workflows/publish.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Publish Package
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v[0-9]+.[0-9]+.[0-9]+'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 30
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-node@v3
15+
with:
16+
node-version: "16"
17+
- run: npm version
18+
- run: npm ci
19+
- run: npm run build
20+
21+
unit-test:
22+
needs: [build]
23+
runs-on: ubuntu-latest
24+
timeout-minutes: 30
25+
steps:
26+
- uses: actions/checkout@v3
27+
- uses: actions/setup-node@v3
28+
with:
29+
node-version: "16"
30+
- run: npm ci
31+
- run: npm run lint
32+
- run: npm run test
33+
34+
publish:
35+
needs: [unit-test]
36+
runs-on: ubuntu-latest
37+
timeout-minutes: 30
38+
steps:
39+
- uses: actions/checkout@v3
40+
- uses: actions/setup-node@v3
41+
with:
42+
node-version: "16"
43+
- run: npm ci
44+
- run: npm run build
45+
- run: npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
46+
env:
47+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
48+
- run: npm publish --access=public
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Version & Changelog
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
version:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 30
12+
steps:
13+
- uses: google-github-actions/release-please-action@v3
14+
with:
15+
release-type: node
16+
package-name: "logger-nodejs"
17+
token: ${{ secrets.RELEASE_PLEASE_TOKEN }}

.gitignore

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port
105+
106+
# Webstorm
107+
.idea/
108+
109+
# MacOS
110+
.DS_Store
111+
112+
build/

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx lint-staged

.lintstagedrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"*.{js,ts,json,yml}": "npm run lint"
3+
}

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": false,
3+
"bracketSpacing": true,
4+
"trailingComma": "es5"
5+
}

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) 2022 Cuckoo Internet
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: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# logger-node
2+
3+
A simple and fast JSON logging library for Node.js services
4+
5+
## Usage
6+
7+
```javascript
8+
import { Logger } from "@cuckoointernet/logger-node";
9+
10+
const logger = new Logger("my-package");
11+
```
12+
13+
### Log simple messages at different levels
14+
15+
```javascript
16+
logger.debug("Hello Cuckoo!");
17+
logger.info("Hello Cuckoo!");
18+
logger.warn("Hello Cuckoo!");
19+
logger.error("Hello Cuckoo!");
20+
logger.fatal("Hello Cuckoo!");
21+
```
22+
23+
### Log additional data via the second argument
24+
25+
```javascript
26+
// Data in object supplied is automatically merged into the log record
27+
logger.info("Hello Cuckoo!", { colour: "yellow" });
28+
```
29+
30+
#### Serialisers
31+
32+
Data provided to the second argument undergoes additional processing if they match certain keys. For instance, if you pass an object with an `error` key it will be run through a serialiser that is able to process stack information in a better way. The standard serialisers are:
33+
34+
| Field | Description |
35+
| ------- | ------------------------------------------------------------------------------------------------------------------------------ |
36+
| `error` | Used for serialising JavaScript error objects, including traversing an error's cause chain for error objects with a `.cause()` |
37+
| `err` | Same as `error` (deprecated) |
38+
| `req` | Common fields from a node.js HTTP request object |
39+
| `res` | Common fields from a node.js HTTP response object |
40+
41+
### Log JavaScript `Error` objects
42+
43+
```javascript
44+
// Alternatively you can supply an instance of Error to log its exception details via the second argument
45+
logger.warn("Sad Cuckoo...", new Error("Wings were clipped!"));
46+
47+
// To log an Error *and* other data at the same time, use the 'error' field name
48+
logger.error("Sad Cuckoo...", {
49+
error: new Error("Wings were clipped!"),
50+
colour: "yellow",
51+
});
52+
```
53+
54+
### Return a JavaScript `Error` after logging
55+
56+
When logging at levels `error` and `fatal` you can return a JavaScript `Error` that has the same message as the log record and then `throw`:
57+
58+
```javascript
59+
// The message of the error thrown will be "Mission failed"
60+
throw logger.error("Mission failed").returnError();
61+
62+
// You can log additional data via the second argument as per usual
63+
throw logger
64+
.fatal("Mission failed", { reason: "Ran out of fuel..." })
65+
.returnError();
66+
```
67+
68+
### Child Loggers
69+
70+
A child logger can be created from an existing one to specialize a logger for a sub-component of your application, i.e. to create a new logger with additional bound fields that will be included in its log records.
71+
72+
```javascript
73+
const parentLogger = new Logger("parent", "debug");
74+
75+
// The child logger inherits the same name and log level as the parent
76+
const childLogger = parentLogger.createChildLogger({
77+
subPackage: "child",
78+
anotherChildField: "whatever-you-want",
79+
});
80+
81+
// All log records will contain the two additional fields setup at initialisation, ie: subPackage & anotherChildField
82+
childLogger.info("Hello from child");
83+
```
84+
85+
## Log Levels
86+
87+
Setting a logger instance to a particular level results in only log records of that level and above being written. You can configure it via the options below:
88+
89+
1. If not specified the logger defaults to `info` level:
90+
91+
```javascript
92+
const logger = new Logger("my-package");
93+
```
94+
95+
2. Set via `logLevel` constructor parameter:
96+
97+
```javascript
98+
const logger = new Logger("my-package", "debug");
99+
```
100+
101+
3. Set via `LOG_LEVEL` environment variable:
102+
103+
```javascript
104+
// process.env.LOG_LEVEL = "debug"
105+
const logger = new Logger("my-package");
106+
```
107+
108+
The available log levels and best practices guidance on when to use them are as follows:
109+
110+
- `fatal` (60): The service/app is going to stop or become unusable now
111+
- `error` (50): Fatal for a particular request, but the service/app continues servicing other requests
112+
- `warn` (40): A note on something that should probably be looked at
113+
- `info` (30): Detail on regular operation
114+
- `debug` (20): Anything else, i.e. too verbose to be included in "info" level
115+
116+
If you want to prevent the logger from printing any messages you can set the log level to `silent`. This is sometimes useful, for example when running tests to reduce noise in the terminal.
117+
118+
## Log Records
119+
120+
The structure of log records is outlined below:
121+
122+
```
123+
{
124+
// User supplied data
125+
name: "my-package",
126+
msg: "Hello Cuckoo!",
127+
128+
...any additional data supplied via second argument to logger methods (see examples above)
129+
130+
// Record metadata (added automatically)
131+
logLevel: "info",
132+
level: 30,
133+
time: "2022-02-03T19:02:57.534Z",
134+
hostname: "banana.local",
135+
pid: 123,
136+
137+
// AWS metadata (added automatically if applicable)
138+
@requestId: <id>
139+
}
140+
```

0 commit comments

Comments
 (0)