Structured Logger With Pino

Configure a structured logger with Pino for high-performance JSON logging with child loggers.

TypescriptCli Tooling

Code

typescriptsnippet
1 line
import pino from'pino';export function createLogger({level='info',service='app',version='1.0.0'}={}){return pino({level,timestamp:pino.stdTimeFunctions.isoTime,formatters:{level(label){return{level:label};}},bindings:{service,version}});}export function childLogger(parent:pino.Logger,context:Record<string,unknown>){return parent.child(context);}export function requestLogger(logger:pino.Logger){return(req:{id:string;method:string;url:string},res:{statusCode:number},next:()=>void)=>{const start=Date.now();next();logger.info({reqId:req.id,method:req.method,url:req.url,status:res.statusCode,duration:Date.now()-start},'request completed');};}

How it works

The working surface is the functions createLogger, childLogger and requestLogger — everything else is wiring.

Watch out for

  • Plan for an install step: pino is a real dependency, resolved by the package manager.

Putting it to use

The entry-point signature is export function createLogger({level='info',service='app',version='1.0.0'}={})

createLogger is what another module would consume; nothing else here is load-bearing for callers.

Interactive Tools for This Topic

FAQ

Does this require external packages?
Yes — `pino`. Add it to your project before running the snippet.