
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
A lightweight, context-aware logger built on top of Pino, using
async_hooksand supporting decorators, DI, and easy context propagation across HTTP or Kafka requests.
userId, traceId, etc., into logsasync_hooks for request-safe storage@WithLogger() for automatic logger injectionpino-prettynpm install logctx
Or with Yarn:
yarn add logctx
import { runWithContext, ContextualLogger } from 'logctx';
const logger = new ContextualLogger();
runWithContext({ userId: '123', traceId: 'xyz' }, () => {
logger.log.info('This will include userId and traceId');
});
@WithLogger() Decoratorimport { WithLogger } from 'logctx';
@WithLogger()
class UserService {
private log: any;
doWork() {
this.log.info('Logged with contextual metadata');
}
}
⚠️ Make sure you use a DI container (like
typedi) orContainer.get(UserService)so decorators are respected.
By default, the logger uses whatever was passed via runWithContext.
But you can override this globally:
import { configureLoggerContext } from 'logctx';
configureLoggerContext(() => ({
userId: 'fallback-user',
tenantId: 'default-tenant'
}));
| Variable | Description | Default |
|---|---|---|
APP_NAME | Name to appear in logs | logctx |
LOG_LEVEL | Logging level (debug, info, warn, error) | info |
LOG_FORMAT | Log output format: json or pretty | json |
LOG_TO_FILE | Enable file logging: true or false | false |
LOG_FILE_PATH | File path to store logs (if LOG_TO_FILE=true) | ./logs/app.log |
runWithContext(context, callback)Wraps a function with the provided context for async propagation.
runWithContext({ userId: 'abc' }, () => {
logger.log.info('userId will be injected');
});
configureLoggerContext(getterFn)Globally defines a fallback context getter if none is set.
configureLoggerContext(() => ({ tenant: 'main', traceId: 'auto' }));
ContextualLoggerProvides a .log object with info, error, warn, and debug.
const logger = new ContextualLogger();
logger.log.info('message');
@WithLogger()Injects this.log into any class. Works well with service or controller patterns.
@WithLogger()
class MyService {
private log: LoggerType; // optional for type hint
doSomething() {
this.log.info('Message with context');
}
}
Here's how you can integrate logctx into an Express application to enable contextual logging based on headers, query parameters, cookies, and route parameters.
import { createContextLogger, ContextualLogger } from "logctx";
import express from "express";
const logger = new ContextualLogger();
// Middleware to initialize context logger
function middleware(
req: express.Request,
res: express.Response,
next: express.NextFunction
) {
createContextLogger(req, next, {
headers: ['x-request-id', 'user-agent'],
queries: ['queryParam'],
cookies: ['sessionId'],
params: ['param1', 'param2'],
});
next(); // Important: call next middleware after context setup
}
const app = express();
app.use(middleware);
// Sample route using contextual logger
app.get('/', (req, res) => {
logger.log.info('Request received');
res.send('Hello from Express with Contextual Logger!');
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
userId, tenantId, traceId across microservicesimport { runWithContext, ContextualLogger } from 'logctx';
const logger = new ContextualLogger();
runWithContext({ userId: 'U001' }, () => {
logger.log.info('User event started'); // includes userId automatically
});
Pull requests, issues, and suggestions welcome!
git clone https://github.com/AjayKrP/logctx.git
npm install
npm run build
FAQs
A Pino-based logger with context-aware logging using async_hooks
We found that logctx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.