logger-core
v0.5.1
Published
logger core
Maintainers
Readme
logger-core
A lightweight, dependency-free JSON logger for Node.js and TypeScript.
- 📄 Structured JSON logging
- 🎯 Configurable log levels
- ⚙️ Runtime configuration
- 🏷 Custom log field names
- 🔖 Constant fields
- 🚀 Fast and simple
- 💡 Written in TypeScript
Unlike traditional text loggers, every log entry is emitted as a JSON object, making it ideal for log aggregation systems such as Elasticsearch, Kibana, Splunk, Datadog, Google Cloud Logging, and AWS CloudWatch.
Installation
npm install logger-coreor
yarn add logger-coreUse Cases
- REST APIs
- Microservices
- Serverless Functions
- Docker Containers
- Kubernetes
- Cloud Run
- AWS Lambda
- Google Cloud Functions
- Azure Functions
Examples:
REST API
- sql-modular-sample: REST API with MySQL.
- mongo-simple-modular-sample: REST API with MongoDB.
Import Data
- import-sample: import a fix-length file to MySql.
- import-csv-sample: import a CSV file to MySql.
Export Data
- postgres-export-sample: export data from Postgres to CSV.
- mssql-export-sample: export data from MS SQL to CSV.
- mysql-export-sample: export data from MySql to CSV.
Message Queue
- rabbitmq-sample: An example to consume message from rabbitmq.
- activemq-sample: An example to consume message from activemq.
- nats-sample: An example to consume message from nats.
Quick Start
import { createLogger } from "logger-core";
const logger = createLogger({});
logger.info("Application started");
logger.warn("Low memory");
logger.error("Database unavailable");Output
{
"level":"info",
"time":"2026-07-02T09:00:00.123Z",
"msg":"Application started"
}Configuration
import { createLogger } from "logger-core";
const logger = createLogger({
level: "INFO"
});Supported levels
| Level | Description | |--------|-------------| | TRACE | Everything | | DEBUG | Debug information | | INFO | General information | | WARN | Warning messages | | ERROR | Errors | | PANIC | Serious errors | | FATAL | Fatal errors |
Logging
logger.trace("trace");
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.panic("panic");
logger.fatal("fatal");Structured Data
Additional properties can be attached to every log.
logger.info("User login", {
userId: 100,
username: "john",
ip: "192.168.1.10"
});Output
{
"level":"info",
"time":"2026-07-02T10:00:00.123Z",
"msg":"User login",
"userId":100,
"username":"john",
"ip":"192.168.1.10"
}Constant Fields
Fields that should appear in every log.
const logger = createLogger({
constants: {
service: "payment-service",
version: "1.0.0",
environment: "production"
}
});Output
{
"service":"payment-service",
"version":"1.0.0",
"environment":"production",
"level":"info",
"time":"...",
"msg":"Application started"
}Custom Field Names
Default fields are
{
"time":"...",
"level":"INFO",
"msg":"..."
}They can be customized.
const logger = createLogger({
map: {
time: "@timestamp",
level: "severity",
msg: "message"
}
});Output
{
"@timestamp":"...",
"severity":"INFO",
"message":"Application started"
}Useful for Elasticsearch and Google Cloud Logging.
Custom Log Level Names
The displayed level names can be changed.
import { createLogger } from "logger-core";
const logger = createLogger({
name: {
trace: "TRACE",
debug: "DEBUG",
info: "INFORMATION",
warn: "WARNING",
error: "ERROR",
panic: "PANIC",
fatal: "FATAL"
}
});Output
{
"level":"INFORMATION",
"time":"...",
"msg":"Application started"
}Runtime Configuration
The logger configuration can be changed without restarting the application.
import * as http from "http";
import { createLogger, LogController } from "logger-core";
const logger = createLogger({
level: "INFO"
});
const controller = new LogController(logger);
http.createServer(controller.config)
.listen(8080);Update log level
POST /config
Content-Type: application/json
{
"level":"DEBUG"
}Update field names
{
"map":{
"time":"@timestamp",
"level":"severity",
"msg":"message"
}
}Update constants
{
"constants":{
"service":"user-service",
"environment":"production"
}
}Custom Output
Logs can be redirected anywhere.
const logger = createLogger(
{},
undefined,
undefined,
console.error,
message => {
// write to Kafka
// write to Elasticsearch
// write to file
// send to Cloud Logging
console.log(message);
}
);API
createLogger()
createLogger(config)Creates a new logger.
Logger
logger.trace()
logger.debug()
logger.info()
logger.warn()
logger.error()
logger.panic()
logger.fatal()isXXXEnabled()
logger.isTraceEnabled()
logger.isDebugEnabled()
logger.isInfoEnabled()
logger.isWarnEnabled()
logger.isErrorEnabled()
logger.isPanicEnabled()
logger.isFatalEnabled()Useful for expensive logging.
if (logger.isDebugEnabled()) {
logger.debug(
expensiveCalculation()
);
}Example Output
{
"service":"payment-service",
"version":"1.2.0",
"environment":"production",
"level":"INFO",
"time":"2026-07-02T09:18:10.123Z",
"msg":"Order created",
"orderId":100,
"userId":55
}Features
- Lightweight
- Zero runtime dependencies
- TypeScript support
- Structured JSON logs
- Runtime configuration
- Custom log levels
- Custom field names
- Constant fields
- Pluggable output
- Node.js compatible
License
MIT
