@aruncodergriphic/logger
v1.0.48
Published
A flexible and production-ready logging library for **microservices**, built on top of [pino](https://github.com/pinojs/pino), with:
Readme
@aruncodergriphic/logger
A flexible and production-ready logging library for microservices, built on top of pino, with:
- Console, File, and Loki transports
- Colorized & pretty console logs
- Structured JSON logs for files & Loki
- Fastify middleware for request/response logging
- GraphQL Apollo plugin for query/mutation/subscription logging
- Built-in helpers for auth, DB, business events
Designed to work independently or as part of any Fastify-based microservice.
🚀 Installation
pnpm install @aruncodergriphic/logger📦 Basic Usage
import { createLogger, logger } from '@aruncodergriphic/logger';
// Use the default logger
logger.info('Service started');
// Create a custom logger
const customLogger = createLogger({
level: 'debug',
service: 'payment-service',
environment: 'production',
});
customLogger.debug('Debugging...');⚡ Features
- Levels:
trace,debug,info,warn,error,fatal - Transports:
- Console (colorized & pretty by default)
- File (with rotation support)
- Loki (Grafana Loki integration, batch & flush)
- Context-aware logging (
requestId,userId, etc.) - Specialized helpers:
logger.request(req, res, duration)logger.database(operation, table, duration)logger.auth(action, userId, success)logger.business(event, entityType, entityId)
🔌 Fastify Integration
import Fastify from 'fastify';
import { logger, fastifyLogger } from '@aruncodergriphic/logger';
const app = Fastify({ logger: false });
app.register(fastifyLogger({
logger,
includeRequestBody: true,
includeResponseBody: false,
excludePaths: ['/health'],
}));
app.get('/ping', async (req, reply) => {
req.log.info('Ping received');
return { pong: true };
});
app.listen({ port: 3000 });🧩 GraphQL Integration (Apollo)
import { ApolloServer } from '@apollo/server';
import { createGraphQLLogger, logger } from '@aruncodergriphic/logger';
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [
createGraphQLLogger({
logger,
logQueries: true,
logMutations: true,
logErrors: true,
logSlowQueries: true,
slowQueryThreshold: 1000, // 1s
}),
],
});📂 File Transport Example
import { createLogger } from '@aruncodergriphic/logger';
const fileLogger = createLogger({
enableFile: true,
transports: {
file: {
level: 'info',
filename: './logs/app.log',
maxSize: '50MB',
maxFiles: 5,
format: 'json'
},
},
});
fileLogger.info('This will be written to app.log');📡 Loki Transport Example
import { createLogger } from '@aruncodergriphic/logger';
const lokiLogger = createLogger({
enableLoki: true,
transports: {
loki: {
level: 'info',
host: 'http://localhost:3100',
basicAuth: {
username: 'admin',
password: 'secret',
},
labels: {
service: 'order-service',
environment: 'staging',
},
batchSize: 100,
flushInterval: 5000,
},
},
});
lokiLogger.info('Hello from Loki transport!');⚙️ Environment Variables
| Variable | Description | Default |
|----------------------|--------------------------------------|-----------------------|
| LOG_LEVEL | Log level (info, debug, etc.) | info |
| SERVICE_NAME | Service name | ai-interview-backend|
| SERVICE_VERSION | Service version | 1.0.0 |
| NODE_ENV | Environment (development, production) | development |
| LOG_FILE | Log file path | ./logs/app.log |
| LOG_MAX_SIZE | Max log file size | 100MB |
| LOG_MAX_FILES | Number of rotated log files | 5 |
| LOKI_URL | Loki server URL | — |
| LOKI_USERNAME | Loki basic auth username | — |
| LOKI_PASSWORD | Loki basic auth password | — |
| LOKI_BATCH_SIZE | Loki batch size | 100 |
| LOKI_FLUSH_INTERVAL| Loki flush interval (ms) | 5000 |
📝 License
MIT © Arun Singh (@aruncodergriphic)
