Log4js
Tips
- By default,
log4js
level fordefault
category is set toOFF
, thus not outputting any logs. To turn on logging,level
must be set other thanOFF
. - Levels: TRACE -> DEBUG -> INFO -> WARN -> ERROR -> FATAL
Glossary
Appenders
- Appenders serialise logs to some form of output.
- Like files, emails, data over the network.
- Frequently used appenders:
stdout
,stderr
,file
(stdout
andstderr
seems to be identical). - Appenders do not specify level.
Categoris
- Categories groups appenders.
- Categories can be inherited by
x.y
syntax(which will inherit fromx
category). - Categories specifies level.
Layout
- Defines structure of each line of code.
- See syntax.
Example
Simplest
1 | var log4js = require("log4js") |
File and stdout combined
1 | const log4js = require("log4js") |
Print error stack trace
1 | logger.error(new Error(errorMessage).stack) |