A powerful log system for node.js, with zero configuration. Built on top of log4js
- Table of Contents
- Features
- Getting Started
- Daemon
- Installation
- API
- Creating Custom Logger
- Default Appenders
- Related Projects
- License
- Easy to get started with zero configuration
- Log files with configurable log rolling based on file size or date
- Different log levels
- Different log categories
- Easy to create custom logger
- All log4js appenders are supported
- Replace native console (disabled by default)
logger
import logger from "pot-logger";
logger.info("pot-logger");
createLogger with custom color
import { createLogger } from "pot-logger";
const foodLogger = createLogger("food", "yellow");
foodLogger.info("虾饺");
All chalk.js colors are available. You can even use dot-notation to define colors. i.e. red.bold
.
createLogger with custom appender
import { createLogger } from "pot-logger";
const appender = {
type: "console",
layout: { type: "pattern", pattern: "\n 🦄 %m 🦄 \n" }
};
const unicornLogger = createLogger("unicorn", appender);
unicornLogger.info("Agnes");
All log4js appenders are available.
By default, logger messages will finaly output by calling console.log
to terminal, but if daemon
mode enabled, logger messages will write to *.log
files.
By default, there are three logs files:
out.log
: Default log file. Only valid log level messages will write to this file.err.log
: AllERROR
orFATAL
log level messages will write to this file.all.log
: All log level messages will write to this file.
To enable daemon
mode, call setConfig('daemon', true)
.
npm i pot-logger
Default logger. A logger is a little bit like console
, it has these methods:
all
(grey)trace
(blue)debug
(cyan)info
(green) [aliaslog
]warn
(yellow)error
(red)fatal
(magenta)mark
(grey)
import { logger } from "pot-logger";
import logger from "pot-logger"; /* or */
var logger = require("pot-logger").logger; /* or */
var logger = require("pot-logger").default; /* or */
Create a custom logger.
category
(String): Logger category.appenderDescription
(String|Object|Function): Please see creating-custom-logger for detail.
Returns a new logger
.
import { createLogger } from "pot-logger";
const logger = createLogger("test", ref => {
return ref.daemon ? ref.defaultDaemonAppender : ref.defaultConsoleAppender;
});
Returns a boolean indicating whether a logger with the specified category exists or not.
category
(String): Logger category.
Returns true
or false
.
Get logger by category. If not found, it would return the default logger.
category
(String): Logger category.
Returns a logger
.
Get logger by category. If not found, create one.
category
(String): Logger category.appenderDescription
(String|Object|Function): Please see creating-custom-logger for detail.
Returns a logger
.
Alias: `setLoggers`
Initialize configure.
keyOrConfig
(String|Object): Config key or config k/v object.value
(Any): Only work if the first argument is aString
.
- enable (Boolean)
- daemon (Boolean)
- logLevel (String|Object)
- logsDir (String)
- overrideConsole (Boolean)
If enable
is false
, no log messages would show, and nothing would write to log files. Defaults to true
.
setConfig("enable", false);
If daemon
is true
, loggers will use *.log
files instead of console
. Defaults to false
.
setConfig("daemon", true);
Defining custom log levels. You could set all categories by passing a level string. Or you could to set some individual categories by passing a key/value object. Defaults to "INFO".
Valid levels: ALL
< TRACE
< DEBUG
< INFO
< WARN
< ERROR
< FATAL
< MARK
< OFF
.
Apply to all categories:
setConfig("logLevel", "DEBUG");
Apply to individual category:
setConfig("logLevel", { myLogger: "DEBUG" });
Defining log files dir. By default, log files will work only when daemon
mode enabled. Defaults to ${cwd}/.logs/
.
setConfig("logsDir", "/my/logs/path/");
Override native console
to logger
. Defaults to false
.
setConfig("overrideConsole", true);
Flush log files.
options
(Object)
removeDir
(Boolean): Remove whole directory. Defaults tofalse
logsDir
(String): Customize logs directory. Defaults toconfig.logsDir
mode
(Number): File mode (permission and sticky bits) when renewing log file. Defaults to0o666
(readable and writable)
Returns a promise
.
Override native console
. Notice that console.log()
will be equal with logger.info()
.
logger
(Object): Defining a logger to overrideconsole
.
Reset console
to the native one. Only work after overrideConsole()
run.
Override native console
in startRun
function runtime.
startRun
(Function): Defining an async function to start to override nativeconsole
. When this function ends,console
will reset to the native one.logger
(Object): Defining a logger to overrideconsole
.
import { overrideConsoleInRuntime } from "pot-logger";
(async function() {
console.log("native"); /* => native */
await overrideConsoleInRuntime(async () => {
console.log("not native"); /* => INFO not native */
});
console.log("native again"); /* => native again */
})();
You could create a custom logger by calling createLogger(category, appenderDescription)
or ensureLogger(category, appenderDescription)
. The appenderDescription
argument is the description of appender.
If appenderDescription
is an <Object>, these options are available:
color
(String) [optional]: The category text color. Support all chalk.js colors. Support dot notation (i.e.red.bold
). Only work for non-daemon (terminal) mode.level
(String) [optional]: Custom log level.maxLevel
(String) [optional]: Custom max log level.file
(Boolean) [optional]: Use new log file or not. Iftrue
, the log file name will be the category name. Defaults tofalse
. Only work for daemon mode.maxLogSize
(Integer) [optional]: The maximum size (in bytes) for the log file. If not specified, then no log rolling will happen. Only work for daemon mode.backups
(Integer) [optional]: The number of old log files to keep during log rolling. Defaults to 5. Only work for daemon mode.compress
(Boolean) [optional] - Compress the backup files during rolling (backup files will have .gz extension). Defaults totrue
. Only work for daemon mode.
import { createLogger } from "pot-logger";
const logger = createLogger("test", {
color: "yellow",
file: true
});
If appenderDescription
is a <String>, it's short for { color: <string\> }
.
If appenderDescription
is a <Function>, there's an argument object which includes:
category
(String)daemon
(Boolean)- defaultDaemonAppender (Object)
- defaultConsoleAppender (Object)
import { createLogger } from "pot-logger";
const logger = createLogger("test", ref => {
return ref.daemon ? ref.defaultDaemonAppender : ref.defaultConsoleAppender;
});
What't more, you could also pass log4js appender configure to appenderDescription
.
{
type: 'file',
filename: defaultCategory,
maxLogSize: 10485760, // 10MB
backups: 5,
compress: true,
}
{
type: 'console',
layout: {
type: 'pattern',
pattern: '%[%p%] %m',
},
}
MIT