Compare to set method, it only add a new appender RATHER THAN replace all of them.
ONLY instance of abstract class Appender will make effect as well.
If the appender has been registered, it won't add a new one.
logger.set([new ConsoleAppender()])
// logger.Appenders.appenders.length === 1
const fileAppender = new FileAppender()
logger.Appender.register(fileAppender)
// logger.Appenders.appenders.length === 2, register a new FileAppender instance
logger.Appender.register(fileAppender)
// logger.Appenders.appenders.length === 2, because it has been registered before
logger.Appender.register(new FileAppender())
// logger.Appenders.appenders.length === 3, a new FileAppender registered
logger.Appender.register({})
// throw a error, because the provided appender is illegal.
delete ;
参数:appender
返回:isSuccess ;
const fileAppender = new FileAppender()
logger.Appender.set([fileAppender])
// logger.Appenders.appenders.length === 1
logger.Appender.delete(new FileAppender())
// === false, logger.Appenders.appenders.length === 1, the given appender doesen't exist
const fileAppender = new FileAppender()
logger.Appender.delete(fileAppender)
// === true, logger.Appenders.appenders.length === 0
Design a custom appender
如果你想自定义一个appender,你只需要从Appender扩展你的类并实现打印方法。
import { appender } from '@8pattern/jlogger'
class CustomAppender extends appender.Appender {
print(logArgs) {
console.log(logArgs)
}
}
jlogger.Appender.register(new CustomAppender())
import JLogger from '@8pattern/jlogger'
const logger = new JLogger()
logger.info('hello, world')
if you execute the previous codes, you will find the console prints a JSON like {"level":"INFO", … ,"count":1,"content":"hello, world"} automatically.
Demo with appenders
import JLogger, { appender } from '@8pattern/jlogger'
const logger = new JLogger()
logger.Appender.register(new appender.FileAppender())
logger.info('hello, world')
Execute the previous codes, not only the console will print a JSON like {"level":"INFO", … ,"count":1,"content":"hello, world"}, but also a log file (yyyy-mm-dd.log) can be found in same dir with the same JSON (in Node environment).
Procedure
Preparation
Before print logs by jLogger, a instance should be generated. It allows users to bind some arguments and configurations on the instance. The construction function can receive two arguments.
Abstract class for every specific appender. Only a abstract method print is binded on it. When you design a custom appender, you MUST extend from this parent class (presented later).
ConsoleAppender
Print the logs into the console. It's also the default appenders, so you won't do anything but work.
FileAppender
Print the logs into the file (only works in Node).
import JLogger, { appender } from '@8pattern/jlogger'
const logger = new JLogger()
logger.Appender.set([new appender.FileAppender()])
It can be instantiated with some configurations as well.
Compare to set method, it only add a new appender RATHER THAN replace all of them.
ONLY instance of abstract class Appender will make effect as well.
If the appender has been registered, it won't add a new one.
logger.set([new ConsoleAppender()])
// logger.Appenders.appenders.length === 1
const fileAppender = new FileAppender()
logger.Appender.register(fileAppender)
// logger.Appenders.appenders.length === 2, register a new FileAppender instance
logger.Appender.register(fileAppender)
// logger.Appenders.appenders.length === 2, because it has been registered before
logger.Appender.register(new FileAppender())
// logger.Appenders.appenders.length === 3, a new FileAppender registered
logger.Appender.register({})
// throw a error, because the provided appender is illegal.
delete <function>
argument: appender
return: isSuccess <boolean>
const fileAppender = new FileAppender()
logger.Appender.set([fileAppender])
// logger.Appenders.appenders.length === 1
logger.Appender.delete(new FileAppender())
// === false, logger.Appenders.appenders.length === 1, the given appender doesen't exist
const fileAppender = new FileAppender()
logger.Appender.delete(fileAppender)
// === true, logger.Appenders.appenders.length === 0
Design a custom appender
If you want to customize a appender, you only need to extend your class from Appender and implement the print method.
import { appender } from '@8pattern/jlogger'
class CustomAppender extends appender.Appender {
print(logArgs) {
console.log(logArgs)
}
}
jlogger.Appender.register(new CustomAppender())
Call to log
logArgument
The logArgument contains all fields of a log, and it may be used to format the logger or print in the appender. It must contains the following values.
level: the log level. Default value: logger.config.level.info.
timeStamp: current time stamp (without formation). Value: Date.now().
count: the number of log called. Value: automatically increased from 0.
content: the log content.
Besides, some custom arguments can be added from construction or log call function. And the arguments from call function has higher priority than the construction. But, "timeStamp" and "count" have the higher priority and can't be overridden.
It is also the sugar of log with error level, but it can receive Error as content. If called with an Error, "errType" and "errDesc" will be added into finalLogArgs, and the stack of Error will be regarded as the content