|
1 | 1 | import os from 'node:os' |
2 | | -import { join, sep } from 'node:path' |
| 2 | +import { sep } from 'node:path' |
3 | 3 | import pico from 'picocolors' |
4 | | -import pino from 'pino' |
5 | 4 |
|
6 | | -const root = join(import.meta.dirname, '..') |
7 | | -export const PATH_TO_PRETTIFYING_PINO_TRANSPORT = join( |
8 | | - root, |
9 | | - 'human-formatter/index.js' |
10 | | -) |
| 5 | +import humanFormatter from '../human-formatter/index.js' |
11 | 6 |
|
12 | 7 | const ERROR_CODES = { |
13 | 8 | EACCES: (e, environment) => { |
@@ -179,40 +174,6 @@ const REPORTERS = { |
179 | 174 | zombie: () => ({ level: 'warn', msg: 'Zombie client was disconnected' }) |
180 | 175 | } |
181 | 176 |
|
182 | | -function createLogger(options) { |
183 | | - if (options.logger === 'human' || options.logger.type === 'human') { |
184 | | - let env = options.env || process.env.NODE_ENV || 'development' |
185 | | - let color = |
186 | | - env !== 'development' ? false : pico.createColors().isColorSupported |
187 | | - let basepath = options.root || process.cwd() |
188 | | - if (basepath.slice(-1) !== sep) basepath += sep |
189 | | - |
190 | | - let logger = pino( |
191 | | - pino.transport({ |
192 | | - options: { |
193 | | - basepath, |
194 | | - color, |
195 | | - destination: options.logger.destination |
196 | | - }, |
197 | | - target: PATH_TO_PRETTIFYING_PINO_TRANSPORT |
198 | | - }) |
199 | | - ) |
200 | | - |
201 | | - // NOTE: needed only for tests |
202 | | - logger._basepath = basepath |
203 | | - logger._color = color |
204 | | - |
205 | | - return logger |
206 | | - } |
207 | | - return pino( |
208 | | - { |
209 | | - name: 'logux-server', |
210 | | - timestamp: pino.stdTimeFunctions.isoTime |
211 | | - }, |
212 | | - options.logger.stream || pino.destination() |
213 | | - ) |
214 | | -} |
215 | | - |
216 | 177 | function cleanFromKeys(obj, regexp, seen) { |
217 | 178 | let result = {} |
218 | 179 | for (let key in obj) { |
@@ -245,12 +206,65 @@ export function createReporter(options) { |
245 | 206 | ) |
246 | 207 | } |
247 | 208 |
|
248 | | - let customLoggerProvided = |
249 | | - typeof options.logger !== 'string' && 'info' in options.logger |
250 | | - if (customLoggerProvided) { |
| 209 | + if (typeof options.logger !== 'string' && 'info' in options.logger) { |
251 | 210 | reporter.logger = options.logger |
252 | 211 | } else { |
253 | | - reporter.logger = createLogger(options) |
| 212 | + let format |
| 213 | + if (options.logger === 'human' || options.logger.type === 'human') { |
| 214 | + let basepath = options.root || process.cwd() |
| 215 | + if (basepath.slice(-1) !== sep) basepath += sep |
| 216 | + let env = options.env || process.env.NODE_ENV || 'development' |
| 217 | + let color = false |
| 218 | + /* c8 ignore next 3 */ |
| 219 | + if (env === 'development') { |
| 220 | + color = pico.createColors().isColorSupported |
| 221 | + } |
| 222 | + if (typeof options.logger === 'object' && options.logger.color) { |
| 223 | + color = true |
| 224 | + } |
| 225 | + format = humanFormatter({ basepath, color }) |
| 226 | + } else { |
| 227 | + format = record => JSON.stringify(record) + '\n' |
| 228 | + } |
| 229 | + let stream = options.logger?.stream ?? process.stderr |
| 230 | + |
| 231 | + function createRecord(level, details, msg) { |
| 232 | + /* c8 ignore next 4 */ |
| 233 | + if (typeof details === 'string') { |
| 234 | + msg = details |
| 235 | + details = {} |
| 236 | + } |
| 237 | + return { |
| 238 | + level, |
| 239 | + time: new Date().toISOString(), |
| 240 | + pid: process.pid, |
| 241 | + ...details, |
| 242 | + msg |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + reporter.logger = { |
| 247 | + /* c8 ignore next 3 */ |
| 248 | + debug(details, msg) { |
| 249 | + stream.write(format(createRecord(20, details, msg))) |
| 250 | + }, |
| 251 | + info(details, msg) { |
| 252 | + stream.write(format(createRecord(30, details, msg))) |
| 253 | + }, |
| 254 | + warn(details, msg) { |
| 255 | + stream.write(format(createRecord(40, details, msg))) |
| 256 | + }, |
| 257 | + error(details, msg) { |
| 258 | + stream.write(format(createRecord(50, details, msg))) |
| 259 | + }, |
| 260 | + fatal(details, msg) { |
| 261 | + if (stream.flushSync) { |
| 262 | + stream.flushSync(format(createRecord(60, details, msg))) |
| 263 | + } else { |
| 264 | + stream.write(format(createRecord(60, details, msg))) |
| 265 | + } |
| 266 | + } |
| 267 | + } |
254 | 268 | } |
255 | 269 | return reporter |
256 | 270 | } |
0 commit comments