@arpinum/log v7.0.0
@arpinum/log 
We are drowning in information but starved for knowledge.
John Naisbitt
@arpinum/log is a simple module to log on stdout or stderr.
Installation
npm install @arpinum/log --saveDefault logger
Just import default logger and start yelling messages:
import { logger } from "@arpinum/log";
logger.info("Something happened");Which outputs :
2017-01-30T09:15:04.821Z - info: [default] Something happenedLogging methods
Available methods are:
- debug
- info
- warn
- error
Each level uses corresponding method on console global object and fallback to console.log if missing for runtime
environment.
Custom logger
You can create a fine tuned logger:
import { createLogger } from "@arpinum/log";
const logger = createLogger({ level: "error" });
logger.info("Something happened");
logger.error("Some error");Which outputs :
2017-01-30T09:23:33.417Z - error: [default] Some errorOptions
You can pass those options during logger creation:
- level
- minimum logging level amongst
all,debug,info,warn,error,off - default is
info ARP_LOG_LEVELenv var may be used to set level
- minimum logging level amongst
- category
- the category displayed in message, useful to filter logs
- default is
default
- fileName
- a category can be created from a base name without extension of a file
- filter
- a regex to filter matching categories
- default is
.*(open bar) ARP_LOG_FILTERenv var may be used to set filter
- getDateString
- a function to get a formatted date
- default function returns
new Date().toISOString() - provide
nullif you do not want a date at all
- getLogInputs
- a function to get inputs given to
consolemethods - used to format message as you want
- see Customize message below
- a function to get inputs given to
Filtering logs
If you have multiple logger instances with various categories, you can filter logs using filter options
or ARP_LOG_FILTER env var.
Example:
// program.js
const mainLogger = createLogger({ category: "main" });
const serviceLogger = createLogger({ category: "service" });
mainLogger.info("Application started");
serviceLogger.info("Doing some stuff");May be run with LOG_FILTER=serv node program.js to output:
2017-01-30T09:32:31.351Z - info: [service] Doing some stuffAbout category:
File name usage:
import { createLogger } from "@arpinum/log";
const logger = createLogger({ fileName: __filename });
logger.info("Something happened");Which outputs :
2017-01-30T09:32:31.351Z - info: [MySuperClass] Something happenedCustomize message
getLogInputs will be called for each log with an object containing:
date: the current date stringcategory: the configured categorylevel: the logger levelargs: all the provided args
Example:
const logger = createLogger({
getLogInputs: ({ date, category, level, args }) => [
`${date}|${category}|${level}`,
...args,
],
});
logger.info("My", "message");
// 2019-11-19T16:45:58.419Z|default|info My messageLicense
11 months ago
6 months ago
1 year ago
1 year ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago