The Logging Framework for JavaScript with log4j configuration writes lines to a file that roll over when they reach a maximum size, or a date/time. You could provide any custom Appenders to write log messages wherever you need, it currently support loggin
Usage no npm install needed!
<script type="module">
import corieLogger from 'https://cdn.skypack.dev/corie-logger';
</script>
README
corie-logger
中文文档
Note: The Logging Framework for JavaScript with log4j configuration writes lines to a file that roll over when they reach a maximum size, or a date/time. You could provide any custom Appenders to write log messages wherever you need, it currently support logging messages to console and files.
'use strict';
module.exports = function (config) {
return function (args, options) {
// options.category logger name
// options.level logger level
// options.timestamp
// return formal args
};
};
API
Global API for corie-logger
configure( opts )
Param
Type
Description
[ opts ]
Object
a JSON Object
[ opts ]
String
a JSON file path
destroy()
destroy internal cache
getLogger( name )
Param
Type
Description
[ name ]
String
get a Logger instance
addLayout( name, serializerGenerator )
Param
Type
Description
[ name ]
String
a name of serializer generator function
[ serializerGenerator ]
Function
a serializer generator function
addAppender( name, Appender )
Param
Type
Description
[ name ]
String
a name of custom Appender
[ Appender ]
Class
a custom Appender
API for Logger instance
setLevels
Param
Type
Description
[ levels ]
String
provide log4j and syslog currently
[ levels ]
Array
custom levels
setLevel
Param
Type
Description
[ levels ]
String
logger level
Configuration
appenders
type - "file", "dateFile" or "console". "file" means to log messages to a file; "dateFile" means to log messages to a file with datetime filename; "console" means to log messages to console.
layout - optional. "basic", "json" or an object(default: "basic"). "json" means to format a message to a JSON string.
fileName - when type is "dateFile", the date pattern must be configured in the file name.
maxLogSize - optional, use case insensitive k、m、g as unit
numBackups - optional, "10d" can be configured that means to back up logs for 10 days
level - optional, log level
categories
appenders - configure different appenders
level - log level
levels - log levels
level - log level
levels - "log4j", "syslog", Array
{
"appenders": {
"log1": { // getLogger('log1')
"type": "file", // log messages to a file
"layout": "json", // optional, not configured or json, or an object
"fileName": "./logs/app.log", // log file path
"maxLogSize": "10M", // optional, use case insensitive k、m、g as unit
"numBackups": 10 // optional, "10d" can be configured that means to back up logs for 10 days
},
"log2": { // getLogger('log2')
"type": "dateFile", // log messages to a file with a datetime file name
"fileName": "./logs/log1-%d{YYYY-MM-DD}.log" // log file path with a datetime pattern
},
"log3": { // getLogger('log3')
"type": "console", // log messages to console
"layout": {
"datePattern": null // default: YYYY-MM-DD hh:mm:ss.SSS Z. null means ISOString formatter
}
}
},
"categories": {
"default": {
"appenders": [
"app"
],
"level": "ERROR"
},
"console": {
"appenders": [
"log2"
],
"level": "INFO"
},
"file": {
"appenders": [
"app",
"log2"
],
"level": "INFO"
},
"dateFile": {
"appenders": [
"log1",
"log2"
],
"level": "INFO"
}
},
"level": "ERROR", // optional, log lovel
"levels": "syslog" // optional(default: "log4j"), "log4j", "syslog"
}