Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit a1f9fc6

Browse files
authored
Use standalone @xpring-eng/logger (#620)
Now that @xpring-eng/logger is a standalone library, use it in the PayID repo.
1 parent aabbbe4 commit a1f9fc6

File tree

3 files changed

+11
-77
lines changed

3 files changed

+11
-77
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
"dependencies": {
3838
"@hapi/boom": "^9.1.0",
3939
"@xpring-eng/http-status": "^1.0.0",
40+
"@xpring-eng/logger": "^1.0.0",
4041
"express": "^4.17.1",
4142
"knex": "^0.21.1",
4243
"knex-stringcase": "^1.4.1",
43-
"log4js": "^6.3.0",
4444
"pg": "8.2.1",
4545
"prom-client": "^12.0.0",
4646
"semver": "^7.3.2",

src/utils/logger.ts

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,7 @@
1-
import { configure, getLogger } from 'log4js'
1+
import createLogger from '@xpring-eng/logger'
22

33
import config from '../config'
44

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)
676

687
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

Comments
 (0)