Structured Logger With Pino
Configure a structured logger with Pino for high-performance JSON logging with child loggers.
Code
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:
pinois 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
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- Does this require external packages?
- Yes — `pino`. Add it to your project before running the snippet.
Report a problem with this exampleOpens a pre-filled issue on GitHub.