Back to all snippets
Library/Node.js/Simple Logger
typescriptbeginnerloggingdebuggingutilities

How to implement Simple Logger in Typescript

Structured logging utility for Node.js

Quick Answer

A structured JSON logger writes machine-parseable log objects with a timestamp and level — making logs instantly queryable in Datadog, CloudWatch, or any log aggregation platform.

Code Snippet

1type LogLevel = 'info' | 'warn' | 'error' | 'debug';
2
3const logger = {
4  log: (level: LogLevel, message: string, meta?: any) => {
5    const timestamp = new Date().toISOString();
6    const log = { timestamp, level, message, ...(meta && { meta }) };
7    console.log(JSON.stringify(log));
8  },
9
10  info: (message: string, meta?: any) => logger.log('info', message, meta),
11  warn: (message: string, meta?: any) => logger.log('warn', message, meta),
12  error: (message: string, meta?: any) => logger.log('error', message, meta),
13  debug: (message: string, meta?: any) => logger.log('debug', message, meta)
14};
15
16export default logger;

What is Simple Logger?

Structured logging outputs JSON objects instead of plain strings, making logs machine-parseable by tools like Datadog, CloudWatch, and ELK. This logger provides the four standard levels — info, warn, error, debug — with automatic timestamps and optional metadata, with zero external dependencies.

How It Works

  1. 1Every log call assembles `{ timestamp, level, message, meta }` into a plain object.
  2. 2`JSON.stringify` serialises it to a single line — log aggregators parse this automatically.
  3. 3The four shorthand methods (`info`, `warn`, `error`, `debug`) delegate to the core `log` function.
  4. 4Optional `meta` object allows structured context alongside the message string.

Common Use Cases

  • API servers - Log every request with method, path, and status code
  • Background jobs - Track job start, completion, and failure with context
  • Error monitoring - Log caught exceptions with stack traces and metadata
  • Audit trails - Record user actions with user ID and timestamp

Key Benefits

  • JSON output is immediately parseable by all major log aggregation platforms
  • Consistent format across all log levels
  • Optional metadata object for structured context
  • Zero dependencies — uses only console.log internally

Common Mistakes to Avoid

  • Using `console.log('message', variable)` in production — unstructured logs are hard to query and alert on.
  • Logging sensitive data like passwords or tokens in the meta object — scrub sensitive fields before logging.
  • Not setting a minimum log level — in production you typically want to suppress `debug` level logs.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more typescript examples

Frequently Asked Questions

Should I use this logger or a library like Winston or Pino?

For production, Pino is the recommended choice — it is extremely fast, has a rich plugin ecosystem, and supports log redaction. This snippet is ideal for simple scripts, lambdas, or projects where adding a dependency is undesirable.

About This Typescript Code Snippet

This free typescript code snippet for simple logger is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this beginner-level example will help you implement simple logger quickly and correctly.

All snippets in the Snippetly library follow typescript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses typescript.

Tags: logging, debugging, utilities  | Language: typescript  | Difficulty: beginner  | Category: Node.js

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.