logger 1.1.1
Install from the command line:
Learn more about npm packages
$ npm install @mocks-server/logger@1.1.1
Install via package.json:
"@mocks-server/logger": "1.1.1"
About this version
Winston-based logger allowing to create nested namespaces. It provides:
- Nested namespaces
- Each namespace includes its "path" in the logs generated by it (the path is formed by its label appended to all its parent labels).
- Coloured logs, with format:
HH:mm:ss:SS [level][...parentLabel:parentLabel:label?] message
- Store logs from each different namespace in a different array.
- Store logs from all namespaces in a global store.
- Each namespace log level can be set separately, and child namespaces can inherit the change or not.
- Emit events whenever a log is added to any store.
A brief example:
const { Logger } = require("@mocks-server/logger");
// Create root logger
const logger = new Logger();
logger.setLevel("debug");
logger.info("Hello world");
// 18:41:43:16 [info] Hello world
const dbLogger = logger.namespace("db");
dbLogger.verbose("Hello from database component");
// 18:41:43:16 [verbose][db] Hello from database component
const dbConnectionLogger = dbLogger.namespace("connection");
dbConnectionLogger.debug("Connecting");
// 18:41:43:16 [debug][db:connection] Connecting
console.log(dbConnectionLogger.store);
/*
[ '18:41:43:16 [debug][db:connection] Connecting' ]
*/
console.log(logger.globalStore);
/*
[
'18:41:43:16 [info] Hello world',
'18:41:43:16 [verbose][db] Hello from database component',
'18:41:43:16 [debug][db:connection] Connecting'
]
*/
const logger = new Logger();
-
Logger(label?, options?)
. Returns alogger
instance.-
label
(String): Label for the root namespace. Optional. -
options
(Object): Optional.-
level
(String): - Initial level, can be one ofsilent
,error
,warn
,info
,verbose
,debug
orsilly
. -
storeLimit
(Number): - Limit of logs to store in the root namespacestore
array. The option will be inherited by all children namespaces. Default is 1000. -
globalStoreLimit
(Number): - Limit of logs to store in the globalstore
array, which stores logs from all nested namespaces. Default is 1000.
-
-
-
error(message)
: The message will be logged always except if the currentlevel
issilent
. -
warn(message)
: The message will be logged always except if the currentlevel
iserror
orsilent
. -
info(message
: The message will be logged whenever the currentlevel
is noterror
,warn
orsilent
. -
verbose(message)
: The message will be logged whenever the currentlevel
is upper or equal thanverbose
. -
debug(message)
: The message will be logged whenever the currentlevel
is upper or equal thandebug
. -
silly(message)
: The message will be logged whenever the currentlevel
is upper or equal thansilly
. -
setLevel(level, [options])
: Sets the logger current log level for the current namespace and all children namespaces recursively.-
level
(String): Level can be one ofsilent
,error
,warn
,info
,verbose
,debug
orsilly
. -
options
(Object):-
transport
(String): TheWinston
transport in which the level has to be set. Can be one ofconsole
orstore
. If not provided, the level is set to all transports. In the root logger, changes in thestore
transport will be applied also to theglobalStore
transport. -
propagate
(Boolean): Propagates the level change to all children namespaces recursively or not. Default istrue
. -
pinned
(Boolean): Whentrue
, next level changes coming from propagations will be ignored and the transport/transports will keep the definedlevel
. Default isfalse
. -
forcePropagation
(Boolean): Whentrue
, the propagation will ignorepinned
levels and will always override them.
-
-
-
namespace(label)
: Creates and returns a new child namespace or returns an already existent one when thelabel
already exists. The returned namespace has the sameLogger
methods described here. The created namespace will inherit the current namespace level.-
label
(String): Label for the new namespace. It will be displayed as part of the log[label]
, appended to parent namespaces labels.
-
-
cleanStore()
Empties the namespace store array. -
onChangeStore(callback)
: Allows to add a listener that will be executed whenever a log is added to the current namespace store. It returns a function that removes the listener once executed.-
callback()
(Function): Callback to be executed.
-
-
onChangeGlobalStore(callback)
: Allows to add a listener that will be executed whenever a log is added to the global store. It returns a function that removes the listener once executed.-
callback()
(Function): Callback to be executed.
-
-
store
: Returns an array with logs stored in the current namespace. -
globalStore
: Returns an array with logs stored in all namespaces, including those from the ancestors. There is only one global namespace for eachLogger
instance, no matter the amount of namespaces it has. -
label
: Getter returning the namespace label. -
level
: Getter returning the current namespace level. -
root
: Getter returning the root namespace instance.
Details
- logger
-
mocks-server
- almost 2 years ago
- MIT
- 3 dependencies
Assets
- logger-1.1.1.tgz
Download activity
- Total downloads 0
- Last 30 days 0
- Last week 0
- Today 0