logger
A Sumor Cloud Tool.
More Documentation
This is a lightweight logger for Node.JS.
It can output logs in different levels, and you can customize the scope, id, and timezone.
Installation
npm i @sumor/logger --save
Prerequisites
Node.JS version
Require Node.JS version 16.x or above
require Node.JS ES module
As this package is written in ES module,
please change the following code in your package.json
file:
{
"type": "module"
}
Usage
General Usage
import Logger from '@sumor/logger'
const logger = new Logger()
logger.trace('Hello World!')
Change Scope
For some case, we need categorize logs. scope
is used for this purpose.
import Logger from '@sumor/logger'
const logger = new Logger({
scope: 'DEMO'
})
logger.trace('Hello World!')
Identifier User
For some case, we need identifier user. id
is used for this purpose.
import Logger from '@sumor/logger'
const logger = new Logger({
id: 'USER001'
})
logger.trace('Hello World!')
Change Level
Most of the time, we only need to output logs of a certain level. Then we can decide if store and display it or not.
import Logger from '@sumor/logger'
const logger = new Logger()
logger.trace('Hello World!')
logger.debug('Hello World!')
logger.info('Hello World!')
logger.warn('Hello World!')
logger.error('Hello World!')
logger.fatal('Hello World!')
Change Timezone
import Logger from '@sumor/logger'
const logger1 = new Logger({
offset: 2 * 60
})
logger1.info('Hello World!')
const logger2 = new Logger({
offset: 8 * 60
})
logger2.info('Hello World!')
Predefined Code
import Logger from '@sumor/logger'
const code = {
trace: {
HTTP_ACCESS: 'The user accesses via HTTP and the IP address is {ip}'
},
debug: {
USER_TOKEN_LOADED: 'The user login information is read and the user ID is {id}'
},
info: {
USER_LOGIN: 'The user logs in and the user ID is {id}'
},
warn: {
USER_LOGOUT: 'The user logs out and the user ID is {id}'
},
error: {
USER_LOGIN_FAILED: 'The user login failed and the user ID is {id}'
},
fatal: {
USER_LOGIN_BLOCKED: 'The user login is blocked and the user ID is {id}'
}
}
const i18n = {
zh: {
USER_LOGIN: '用户登录,用户ID为{id}'
}
}
const logger1 = new Logger({
code,
i18n
})
logger1.code('USER_LOGIN', { id: 'USER001' })
const logger2 = new Logger({
code,
i18n,
language: 'zh-US'
})
logger2.code('USER_LOGIN', { id: 'USER001' })
const logger3 = new Logger({
code,
i18n,
language: 'zh-CN'
})
logger3.code('USER_LOGIN', { id: 'USER001' })
Global Language Setting
process.env.LANGUAGE = 'zh-CN'
import Logger from '@sumor/logger'
const code = {
info: {
USER_LOGIN: 'The user logs in and the user ID is {id}'
}
}
const i18n = {
zh: {
USER_LOGIN: '用户登录,用户ID为{id}'
}
}
const logger = new Logger({
code,
i18n
})
logger.code('USER_LOGIN', { id: 'USER001' })
Filter Level
When you want to filter logs by level, you can use the following code:
import Logger from '@sumor/logger'
const logger = new Logger({
level: 'info'
})
logger.trace('Hello World!')
logger.debug('Hello World!')
logger.info('Hello World!')
logger.warn('Hello World!')
logger.error('Hello World!')
logger.fatal('Hello World!')
When you use this library cross multiple libraries, you can use the following code:
import Logger from '@sumor/logger'
const logger = new Logger()
process.env.LOG_LEVEL = 'info'
logger.trace('Hello World!')
logger.debug('Hello World!')
logger.info('Hello World!')
logger.warn('Hello World!')
logger.error('Hello World!')
process.env.LOG_LEVEL = 'warn'
logger.info('Hello World!')