|
1 | | -import { configure, getLogger } from 'log4js' |
| 1 | +import createLogger from '@xpring-eng/logger' |
2 | 2 |
|
3 | 3 | import config from '../config' |
4 | 4 |
|
5 | | -// Log Levels |
6 | | -// |
7 | | -// OFF Designates no log messages should be shown |
8 | | -// MARK Designates an event that will almost always be logged. Useful for temporary logging. |
9 | | -// FATAL Designates very severe error events that will presumably lead the application to abort. |
10 | | -// ERROR Designates error events that might still allow the application to continue running. |
11 | | -// WARN Designates potentially harmful situations. |
12 | | -// INFO Designates informational messages that highlight the progress of the application at coarse-grained level. |
13 | | -// DEBUG Designates fine-grained informational events that are most useful to debug an application. |
14 | | -// TRACE Designates finer-grained informational events than the DEBUG. |
15 | | -// ALL Designates all log events should be shown (equivalent to TRACE) |
16 | | - |
17 | | -enum LogLevel { |
18 | | - OFF = 'OFF', |
19 | | - MARK = 'MARK', |
20 | | - FATAL = 'FATAL', |
21 | | - ERROR = 'ERROR', |
22 | | - WARN = 'WARN', |
23 | | - INFO = 'INFO', |
24 | | - DEBUG = 'DEBUG', |
25 | | - TRACE = 'TRACE', |
26 | | - ALL = 'ALL', |
27 | | -} |
28 | | - |
29 | | -// Get the current log level from configuration |
30 | | -const LOG_LEVEL = config.app.logLevel.toUpperCase() |
31 | | - |
32 | | -if (!isLogLevel(LOG_LEVEL)) { |
33 | | - throw new Error( |
34 | | - `The log level '${LOG_LEVEL}' specified in config is not an acceptable log level`, |
35 | | - ) |
36 | | -} |
37 | | - |
38 | | -configure({ |
39 | | - appenders: { |
40 | | - out: { |
41 | | - type: 'stdout', |
42 | | - |
43 | | - // Pattern Format documentation: https://github.com/log4js-node/log4js-node/blob/master/docs/layouts.md#pattern-format |
44 | | - layout: { type: 'pattern', pattern: '%[%-5p%] %m' }, |
45 | | - }, |
46 | | - |
47 | | - // Define a custom pattern to use when LOG_LEVEL=TRACE that includes filenames and line numbers in the log |
48 | | - trace: { |
49 | | - type: 'stdout', |
50 | | - layout: { type: 'pattern', pattern: '%[%-5p %f:%-3l%]\t%m' }, |
51 | | - }, |
52 | | - }, |
53 | | - categories: { |
54 | | - default: { appenders: ['out'], level: LOG_LEVEL }, |
55 | | - trace: { appenders: ['trace'], level: LOG_LEVEL, enableCallStack: true }, |
56 | | - }, |
57 | | -}) |
58 | | - |
59 | | -let logCategory = 'default' |
60 | | - |
61 | | -// If LOG_LEVEL = TRACE|ALL, use the logger that shows filenames and line numbers |
62 | | -if ([LogLevel.TRACE, LogLevel.ALL].includes(LOG_LEVEL)) { |
63 | | - logCategory = 'trace' |
64 | | -} |
65 | | - |
66 | | -const logger = getLogger(logCategory) |
| 5 | +const logger = createLogger(config.app.logLevel) |
67 | 6 |
|
68 | 7 | export default logger |
69 | | - |
70 | | -// HELPER FUNCTIONS |
71 | | -// TODO:(hbergren) Could possibly make this a generic (for stringy enums, but not numeric enums) |
72 | | -/** |
73 | | - * A type guard function that determines if a provided string is a LogLevel enum member. |
74 | | - * |
75 | | - * @param member - A string which may or may not be a LogLevel. |
76 | | - * |
77 | | - * @returns A type predicate (boolean for type narrowing) indicating whether the string is a LogLevel. |
78 | | - */ |
79 | | -function isLogLevel(member: string): member is LogLevel { |
80 | | - return Object.values(LogLevel).includes(member as LogLevel) |
81 | | -} |
0 commit comments