@a14313/logger
v1.3.1
Published
A lightweight, zero-config wrapper for pino, the fastest Node.js logger. Get a production-ready logger instance instantly with sensible defaults, automatic pretty-printing in development, and built-in context, allowing you to skip the setup and start logg
Readme
@a14313/logger
A lightweight, zero-config wrapper for pino, the fastest Node.js logger. Get a production-ready logger instance instantly with sensible defaults, automatic pretty-printing in development, and built-in context, allowing you to skip the setup and start logging immediately.
Installation
NPM
npm i @a14313/loggerPNPM
pnpm i @a14313/loggerUsage
By default, you don't need to instantiate the logger. When you import the logger, it is automatically instantiated. However, you can import the Logger class if you want to instantiate it yourself. Example: const newLogger = new Logger();. But to make it simpler, just use the logger with the small L.
Setup
Set these variables to your environment:
LOG_LEVEL=debug
NODE_ENV=devLOG_LEVEL can have these values: debug | info | warn | error | fatal
NODE_ENV can have these values: dev | development | prod | prd | production
CommonJS
const logger = require('@a14313/logger');
logger.debug('This is debug');
logger.info('This is info with extra data', {
foo: 'bar',
baz: 1,
bool: true,
});
logger.warn('This is Warning!');
logger.error('This is Error!');
logger.fatal('This is Fatal!');ESM Modules
If you are having issues importing it from Typescript or ES6 codes, go to the troubleshooting guide.
import logger from '@a14313/logger';
logger.debug('This is debug');
logger.info('This is info with extra data', {
foo: 'bar',
baz: 1,
bool: true,
});
logger.warn('This is Warning!');
logger.error('This is Error!');
logger.fatal('This is Fatal!');Sample output
Development

[2025-12-03 01:45:35.835 UTC] DEBUG (17472 on LPHLIFEFS8ZDY3): BAx1cWEBAjVzO_wDjvvXw This is debug
[2025-12-03 01:45:35.837 UTC] INFO (17472 on LPHLIFEFS8ZDY3): xNjfGrcjM_nHF3FWOM-8L This is info with extra data
data: {
"foo": "bar",
"baz": 1,
"bool": true
}
[2025-12-03 01:45:35.837 UTC] WARN (17472 on LPHLIFEFS8ZDY3): qxRedJg6X3ve_OA5a3Xun This is Warning!
[2025-12-03 01:45:35.837 UTC] ERROR (17472 on LPHLIFEFS8ZDY3): xa1Vc93OKNcq9p14-3X3m This is Error!
[2025-12-03 01:45:35.837 UTC] FATAL (17472 on LPHLIFEFS8ZDY3): XeLmK1E3YY-kPGBehiYG3 This is Fatal!Production

{"level":20,"time":"2025-12-03T01:46:58.616Z","pid":36532,"hostname":"LPHLIFEFS8ZDY3","traceId":"oiZ0bVy86qimGtk_KYXSC"
,"msg":"This is debug"}
{"level":30,"time":"2025-12-03T01:46:58.618Z","pid":36532,"hostname":"LPHLIFEFS8ZDY3","traceId":"swux-WaDziXWy_jcCP_Y1"
,"msg":"This is info with extra data","data":{"foo":"bar","baz":1,"bool":true}}
{"level":40,"time":"2025-12-03T01:46:58.618Z","pid":36532,"hostname":"LPHLIFEFS8ZDY3","traceId":"LxFi797eny5F7-2UcQ2EY"
,"msg":"This is Warning!"}
{"level":50,"time":"2025-12-03T01:46:58.618Z","pid":36532,"hostname":"LPHLIFEFS8ZDY3","traceId":"7NgeGuNWdIOt6xnT7-20K"
,"msg":"This is Error!"}
{"level":60,"time":"2025-12-03T01:46:58.618Z","pid":36532,"hostname":"LPHLIFEFS8ZDY3","traceId":"lsp3XCMfEWGYvv3IrFN5-"
,"msg":"This is Fatal!"}Instantiating your own Logger
If you decided to instantiate your Logger, you can override the defaults.
const myLogger = new Logger({options})
Options
| Parameter | Type | Required | Default | Description |
| :-------- | :-------- | :------- | :--------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| logLevel | String | No | info | These are the valid values for logLevel: debug | info | warn | error | fatal |
| usePretty | Boolean | No | Depends on the environment. If the environment set on the env is prod, the default is false otherwise true | This will override the default logger if it will use json or pino-pretty regardless of the environment. WARNING⚠️: It is not recommended to use pretty logs on prod. It is resource intensive and the log processors might not be able to read your logs properly. Use this on dev environment only. Or better if you use the default, because it will automatically adjust depending on the environment. 😉 |
const { Logger } = require('@a14313/logger');
// or
// import { Logger } from '@a14313/logger';
const newLogger = new Logger({
logLevel: 'info',
usePretty: true,
});
newLogger.debug('This is debug');
newLogger.info('This is info');Integration with pino-http (For backend)
When you need to integrate on pino-http to get the http requests, you need to configure the pino-http to use your existing logger. Just use the logger.raw or if you instantiated your own logger, myNewLogger.raw to use the installed logger a14313/logger, and that's it.
const logger = require('@a14313/logger');
pinoHttp({
logger: logger.raw,
// ... The rest of the config
});Fixing some issues with the Typescript
You can use this technique if you are having problems importing it. Otherwise, don't use this.
If you ever came to a point where you cannot import it properly on your typescript or ES6 project, that is because I exported this package with plain vanilla javascript in mind, for backwards compatibility. So that even on commonjs, you can use this.
Fix
Solution 1
For maximum compatibility on typescript just add this on your tsconfig.json.
{
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}This will allow you to import like a native ES6.
import logger from '@a14313/logger';Solution 2
If you don't want to do the Solution 1 or you cannot modify the tsconfig, try this instead:
// First import the type of the Logger
import type { Logger } from '@a14313/logger';
// Use the "require" syntax and add the type "Logger".
// Then add this comment:
// eslint-disable-next-line @typescript-eslint/no-var-requires
const logger: Logger = require('@a14313/logger');
// Then you can use the logger as it is:
logger.debug('Debug log');
logger.info('Info log');Author
The author of this API has an author to his name 😄.
