|
1 |
| -# Logardian |
| 1 | +# NestJS Response Structure |
2 | 2 |
|
3 |
| -Inspired by NestJS Logger, Logardian was built to output minimalistic, readable logs. |
| 3 | +GraphQL Types and interfaces for great response |
4 | 4 |
|
5 |
| -## Roadmap |
6 |
| - |
7 |
| -- Logging to file |
8 |
| - |
9 |
| - |
10 | 5 | ## Installation
|
11 | 6 |
|
12 | 7 | Requires node version >=12.х
|
13 | 8 |
|
14 | 9 | ```bash
|
15 |
| -npm i --save logardian |
16 |
| -``` |
17 |
| - |
18 |
| -## Features |
19 |
| - |
20 |
| -- Various layers of logs that can be turned on/off via .env (`LOGARDIAN_LABELS`) |
21 |
| -- In production mode debug() does not work |
22 |
| -- Datetime in UTC format |
23 |
| -- Format of logs: `[time] [level] [layer] [message]` |
24 |
| -- In debug mode the path and name of the function that called the log is displayed |
25 |
| -- Can be used instead of NestJS Logger |
26 |
| -- Can log any objects, arrays, variables |
27 |
| -- Modes: normal or json format output |
28 |
| -- Colors! |
29 |
| - |
30 |
| - |
31 |
| -## Usage/Examples |
32 |
| - |
33 |
| -```ts |
34 |
| -import Logardian from 'logardian' |
35 |
| - |
36 |
| -const logger = new Logardian() |
37 |
| - |
38 |
| -logger.configure({ |
39 |
| - trace: false |
40 |
| -}) |
41 |
| - |
42 |
| -logger.log(`Hi! I'm info log example`) |
43 |
| -logger.warn(`Hi! I'm warn log example`, { trace: false }) |
44 |
| -logger.error(`Hi! I'm error log example`) |
45 |
| -logger.verbose(`Hi! I'm verbose log example`, { label: 'database' }) |
46 |
| -logger.debug(`Hi! I'm debug log example`, { some: 'object' }) |
47 |
| -``` |
48 |
| - |
49 |
| -***default output:*** |
50 |
| - |
51 |
| - |
52 |
| - |
53 |
| - |
54 |
| -***json output:*** |
55 |
| - |
56 |
| -```ts |
57 |
| -logger.configure({ |
58 |
| - json: true |
59 |
| -}) |
60 |
| -``` |
61 |
| - |
62 |
| -```bash |
63 |
| -{ |
64 |
| - "timestamp": "2021-11-05T05:54:10.920Z", |
65 |
| - "message": "Hi! I'm info log example", |
66 |
| - "level": "log" |
67 |
| -} |
68 |
| -{ |
69 |
| - "timestamp": "2021-11-05T05:54:10.920Z", |
70 |
| - "message": "Hi! I'm warn log example", |
71 |
| - "level": "warn" |
72 |
| -} |
73 |
| -{ |
74 |
| - "timestamp": "2021-11-05T05:54:10.920Z", |
75 |
| - "message": "Hi! I'm error log example", |
76 |
| - "level": "error" |
77 |
| -} |
78 |
| -{ |
79 |
| - "timestamp": "2021-11-05T05:54:10.920Z", |
80 |
| - "message": "Hi! I'm verbose log example", |
81 |
| - "level": "verbose", |
82 |
| - "label": "database" |
83 |
| -} |
84 |
| -{ |
85 |
| - "timestamp": "2021-11-05T05:54:10.920Z", |
86 |
| - "message": "{\n \"some\": \"object\"\n}", |
87 |
| - "level": "debug" |
88 |
| -} |
89 |
| -``` |
90 |
| - |
91 |
| -## Environment Variables |
92 |
| - |
93 |
| -`NODE_ENV` production start does not show debug() logs |
94 |
| - |
95 |
| -## FAQ |
96 |
| - |
97 |
| -#### How does it implement NestJS Logger without any framework libs? |
98 |
| - |
99 |
| -We made logger based on [LoggerService](https://github.com/nestjs/nest/blob/master/packages/common/services/logger.service.ts) but we don't explicitly import it so that we stay dependless of NestJS libraries. But you can also use the Logardian instead of common NestJS logger. |
100 |
| - |
101 |
| -```ts |
102 |
| -// main.ts |
103 |
| -import { Logardian } from 'logardian' |
104 |
| - |
105 |
| -const logger = new Logardian() |
106 |
| - |
107 |
| -async function bootstrap(): Promise<void> { |
108 |
| - const app = await NestFactory.create(AppModule, { logger }) |
109 |
| - |
110 |
| - await app.listen(port, hostname, () => |
111 |
| - logger.log(`Server running at ${hostname}:${port}`), |
112 |
| - ) |
113 |
| -} |
114 |
| -``` |
115 |
| - |
116 |
| -#### How can I use logardian in my service? |
117 |
| - |
118 |
| -Simply create a new logger class |
119 |
| - |
120 |
| -```ts |
121 |
| -import { Logardian } from 'logardian' |
122 |
| - |
123 |
| -@Injectable() |
124 |
| -export class CatService { |
125 |
| - private readonly _logger = new Logardian() |
126 |
| -} |
| 10 | +npm i --save nestjs-response-structure |
127 | 11 | ```
|
128 | 12 |
|
129 |
| -#### I do not see my logs with label |
130 |
| - |
131 |
| -Specify labels you want to log or write `*` to log every log with label. |
132 |
| -Working in production and development mode |
133 |
| - |
134 |
| -Logardian is a singleton, so it means that `configure()` works on all Logardian instances |
135 |
| - |
136 |
| -```ts |
137 |
| -import { Logardian } from 'logardian' |
138 |
| - |
139 |
| -const logger = new Logardian() |
140 |
| - |
141 |
| -logger.configure({ |
142 |
| - labels: '*' // or ['database', 'events'] or false |
143 |
| - trace: false, |
144 |
| - json: true |
145 |
| -}) |
146 |
| -``` |
147 |
| - |
148 |
| -#### I do not want to see caller and path. How can I turn off them globally? |
149 |
| - |
150 |
| -Specify 'false' on logardian config. If you specify `trace: true` in logger function trace will log in spite of config option |
151 |
| - |
152 |
| -Priority of trace from high to low: |
153 |
| - |
154 |
| -1. Production mode |
155 |
| -2. `logger.log('Hello', { trace: true })` |
156 |
| -3. `logger.configure({ trace: false })` |
157 |
| - |
158 |
| - |
159 |
| - |
160 | 13 | ## License
|
161 | 14 |
|
162 | 15 |
|
|
0 commit comments