Skip to content

Commit 9f52899

Browse files
committed
v3 refactoring
1 parent e905c46 commit 9f52899

File tree

16 files changed

+560
-724
lines changed

16 files changed

+560
-724
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
node_modules
2-
logs
1+
/node_modules
2+
/logs
3+
/temp
4+
/lib

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ test.js
44
package-lock.json
55
tsconfig.json
66
.gitignore
7+
temp
8+
logs

README.md

Lines changed: 6 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,17 @@
1-
# Simple Logger
1+
# Logger v3
22

33
[![npm version](https://badge.fury.io/js/%40nolawnchairs%2Flogger.svg)](https://badge.fury.io/js/%40nolawnchairs%2Flogger)
44

5-
A simple, zero-dependency NodeJS logger with customizable colors and output format to use across your entire Node app. Fun!
5+
Version 3.x is incompatible with 2.x and 1.x. Note that this project is more or less an "in-house" tool, so no guarantees.
66

7-
Version 2.x is not backwards-compatible with versions 1.x
7+
For v2 docs, go [here](V2.md)
88

9-
### Install
9+
## Install
1010
```
1111
npm i @nolawnchairs/logger
1212
yarn add @nolawnchairs/logger
1313
```
1414

15-
### Usage
15+
## Setting Logging Up
1616

17-
```
18-
import { Logger, LogLevel } from '@nolawnchairs/logger
19-
20-
const logger = new Logger(LogLevel.Debug)
21-
22-
logger.info('this is an info message')
23-
logger.error('this is an ERROR level logging')
24-
```
25-
26-
Since under the hood, this is simply an abstraction over `console.log` and `console.error`, you can add string formatting to the first argument
27-
```
28-
logger.info('You can use variables like this: %d', 42)
29-
...
30-
31-
2020-05-27T19:45:58.468Z 19027 INFO | You can use variables like this: 42
32-
```
33-
34-
### Format
35-
36-
The default format is as follows:
37-
38-
```
39-
logger.info('This is the message!')
40-
...
41-
42-
2020-05-27T19:45:58.468Z 19027 INFO | This is the message!
43-
```
44-
45-
Logging format can be customized by using the second constructor parameter, which is a function that provides two arguments: `entry` and `colorizer`
46-
47-
48-
```
49-
const logger = new Logger(LogLevel.Debug, (entry, colorizer) => {
50-
return `[${e.levelValue.trim()}] - ${e.date.toIsoString()} ${e.pid} ${e.message}`
51-
})
52-
53-
logger.info('This is the message!')
54-
...
55-
56-
[INFO] 2020-05-27T19:45:58.468Z 19027 This is the message!
57-
```
58-
59-
### LogEntry
60-
The log entry contains the following values:
61-
62-
| Key | Type | Description |
63-
|-|-|-|
64-
| `date`| Date | Javascript Date object |
65-
| `level` | LogLevel | `LogLevel` enum value |
66-
| `levelValue` | string | string representation of the logging level. The INFO and WARN strings are end-padded with a space for symmetry with ERROR and DEBUG |
67-
| `pid` | string | string value of process ID |
68-
| `message` | string | the formatted message |
69-
70-
71-
### Colorizer
72-
The colorizer object contains static functions to colorize output using ANSI colors
73-
74-
| Function | Description |
75-
|-|-|
76-
| `green` | Colors the text green, default for `DEBUG ` |
77-
| `cyan` | Colors the text light blue (cyan), default for `INFO` |
78-
| `yellow` | Colors the text yellow, default for `WARN` |
79-
| `red` | Colors the text with red background and white text, default for `ERROR` |
80-
| `grey` | Colors the text grey, default for the date |
81-
| `custom` | Customizes the color. The first argument is the text to be colored, the second is the ANSI code's value which is between the `[` and the `m`. For `\x1b[41m`, pass `41`, for `\x1b[30;1m`, pass `30;1` |
82-
| `levelDefault` | Colors the text the default color of the `LogLevel` provided in the first argument. The second (optional argument) is the text to print. This can be any text, but if left undefined, it will print the default level text value (e.g. `INFO `) with the default end padding
83-
84-
An example using the custom formatter that prints the first character of the level only, followed by the date, pid a pipe, then the message:
85-
86-
```
87-
const customLogger = new Logger(LogLevel.Debug, (entry, colorizer) => {
88-
return [
89-
`[${colorizer.levelDefault(entry.level, entry.levelValue.charAt(0))}]`,
90-
colorizer.grey(entry.date.toISOString()),
91-
entry.pid,
92-
'|',
93-
entry.message
94-
].join(' ')
95-
})
96-
97-
customLogger.info('This is a test message')
98-
...
99-
100-
[I] 2020-07-09T20:57:24.670Z 26816 | This is a test message
101-
```
102-
103-
## Expanding
104-
105-
**`Logger.expand()`**
106-
107-
Some objects you want to log are complex data structures with nested arrays and objects. By default, the first argument to any log call is not inspected, but subsequent args added will all be inspected to a reasonable depth. To allow the capability of printing out these objects fully, you can expand the logger.
108-
109-
```
110-
const sample = {
111-
name: 'Rick Sanchez',
112-
intelligence: 10,
113-
sidekick: {
114-
name: 'Morty Smith',
115-
intelligence: 2,
116-
}
117-
}
118-
119-
Log.info(sample)
120-
...
121-
122-
[object Object]
123-
124-
Log.expand().info(sample)
125-
...
126-
{ name: 'Rick Sanchez',
127-
intelligence: 10,
128-
sidekick: { name: 'Morty Smith', intelligence: 2 } }
129-
```
130-
131-
All varargs are already expanded by default. This has a limited depth of `2`, so you can expand to the desired depth. Circular references are always represented as `[Circular]`.
132-
133-
```
134-
Log.info('Sample', sample)
135-
...
136-
137-
Sample { name: 'Rick Sanchez',
138-
intelligence: 10,
139-
sidekick: { name: 'Morty Smith', intelligence: 2 } }
140-
```
141-
## Forcing Level
142-
143-
**`Logger.force()`**
144-
145-
There may be times where you wish to print an `INFO` level message when your level is set to a higher level such as `WARN` or `ERROR`. This is just sugar for creating a new logger instance with level `DEBUG`
146-
147-
```
148-
logger.force().info('This is an info message that is guaranteed to print')
149-
```
150-
151-
### License
152-
Use and abuse it however you like
153-
17+
As opposed to v2, there is more boilerplate to configure, since we now provide multiple logging environments that can each write to multiple output streams.

V2.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Simple Logger
2+
3+
[![npm version](https://badge.fury.io/js/%40nolawnchairs%2Flogger.svg)](https://badge.fury.io/js/%40nolawnchairs%2Flogger)
4+
5+
A simple, zero-dependency NodeJS logger with customizable colors and output format to use across your entire Node app. Fun!
6+
7+
Version 2.x is not backwards-compatible with versions 1.x
8+
9+
### Install
10+
```
11+
npm i @nolawnchairs/logger
12+
yarn add @nolawnchairs/logger
13+
```
14+
15+
### Usage
16+
17+
```
18+
import { Logger, LogLevel } from '@nolawnchairs/logger
19+
20+
const logger = new Logger(LogLevel.Debug)
21+
22+
logger.info('this is an info message')
23+
logger.error('this is an ERROR level logging')
24+
```
25+
26+
Since under the hood, this is simply an abstraction over `console.log` and `console.error`, you can add string formatting to the first argument
27+
```
28+
logger.info('You can use variables like this: %d', 42)
29+
...
30+
31+
2020-05-27T19:45:58.468Z 19027 INFO | You can use variables like this: 42
32+
```
33+
34+
### Format
35+
36+
The default format is as follows:
37+
38+
```
39+
logger.info('This is the message!')
40+
...
41+
42+
2020-05-27T19:45:58.468Z 19027 INFO | This is the message!
43+
```
44+
45+
Logging format can be customized by using the second constructor parameter, which is a function that provides two arguments: `entry` and `colorizer`
46+
47+
48+
```
49+
const logger = new Logger(LogLevel.Debug, (entry, colorizer) => {
50+
return `[${e.levelValue.trim()}] - ${e.date.toIsoString()} ${e.pid} ${e.message}`
51+
})
52+
53+
logger.info('This is the message!')
54+
...
55+
56+
[INFO] 2020-05-27T19:45:58.468Z 19027 This is the message!
57+
```
58+
59+
### LogEntry
60+
The log entry contains the following values:
61+
62+
| Key | Type | Description |
63+
|-|-|-|
64+
| `date`| Date | Javascript Date object |
65+
| `level` | LogLevel | `LogLevel` enum value |
66+
| `levelValue` | string | string representation of the logging level. The INFO and WARN strings are end-padded with a space for symmetry with ERROR and DEBUG |
67+
| `pid` | string | string value of process ID |
68+
| `message` | string | the formatted message |
69+
70+
71+
### Colorizer
72+
The colorizer object contains static functions to colorize output using ANSI colors
73+
74+
| Function | Description |
75+
|-|-|
76+
| `green` | Colors the text green, default for `DEBUG ` |
77+
| `cyan` | Colors the text light blue (cyan), default for `INFO` |
78+
| `yellow` | Colors the text yellow, default for `WARN` |
79+
| `red` | Colors the text with red background and white text, default for `ERROR` |
80+
| `grey` | Colors the text grey, default for the date |
81+
| `custom` | Customizes the color. The first argument is the text to be colored, the second is the ANSI code's value which is between the `[` and the `m`. For `\x1b[41m`, pass `41`, for `\x1b[30;1m`, pass `30;1` |
82+
| `levelDefault` | Colors the text the default color of the `LogLevel` provided in the first argument. The second (optional argument) is the text to print. This can be any text, but if left undefined, it will print the default level text value (e.g. `INFO `) with the default end padding
83+
84+
An example using the custom formatter that prints the first character of the level only, followed by the date, pid a pipe, then the message:
85+
86+
```
87+
const customLogger = new Logger(LogLevel.Debug, (entry, colorizer) => {
88+
return [
89+
`[${colorizer.levelDefault(entry.level, entry.levelValue.charAt(0))}]`,
90+
colorizer.grey(entry.date.toISOString()),
91+
entry.pid,
92+
'|',
93+
entry.message
94+
].join(' ')
95+
})
96+
97+
customLogger.info('This is a test message')
98+
...
99+
100+
[I] 2020-07-09T20:57:24.670Z 26816 | This is a test message
101+
```
102+
103+
## Expanding
104+
105+
**`Logger.expand()`**
106+
107+
Some objects you want to log are complex data structures with nested arrays and objects. By default, the first argument to any log call is not inspected, but subsequent args added will all be inspected to a reasonable depth. To allow the capability of printing out these objects fully, you can expand the logger.
108+
109+
```
110+
const sample = {
111+
name: 'Rick Sanchez',
112+
intelligence: 10,
113+
sidekick: {
114+
name: 'Morty Smith',
115+
intelligence: 2,
116+
}
117+
}
118+
119+
Log.info(sample)
120+
...
121+
122+
[object Object]
123+
124+
Log.expand().info(sample)
125+
...
126+
{ name: 'Rick Sanchez',
127+
intelligence: 10,
128+
sidekick: { name: 'Morty Smith', intelligence: 2 } }
129+
```
130+
131+
All varargs are already expanded by default. This has a limited depth of `2`, so you can expand to the desired depth. Circular references are always represented as `[Circular]`.
132+
133+
```
134+
Log.info('Sample', sample)
135+
...
136+
137+
Sample { name: 'Rick Sanchez',
138+
intelligence: 10,
139+
sidekick: { name: 'Morty Smith', intelligence: 2 } }
140+
```
141+
## Forcing Level
142+
143+
**`Logger.force()`**
144+
145+
There may be times where you wish to print an `INFO` level message when your level is set to a higher level such as `WARN` or `ERROR`. This is just sugar for creating a new logger instance with level `DEBUG`
146+
147+
```
148+
logger.force().info('This is an info message that is guaranteed to print')
149+
```
150+
151+
### License
152+
Use and abuse it however you like
153+

0 commit comments

Comments
 (0)