Introduction: Why Do You Need Logging in TypeScript?
Logging is the process of recording events that occur in an application during its operation. For a TypeScript developer, this is not just an addition, but a crucial tool for debugging, monitoring, and analyzing program behavior. Without properly configured logging, you risk spending hours searching for bugs that could have been found in minutes.
In this article, we will break down the best logging practices for beginner TypeScript developers. You will learn how to go beyond simple console.log, which libraries to use, and how to structure logs so they provide real value.
1. Logging Basics: From console.log to Structured Data
Many beginners start with console.log('Something went wrong'). It works, but quickly turns into chaos. In modern TypeScript development, it is common to use structured logging.
Problems with Simple console.log
- Lack of levels: You cannot filter errors from debug messages.
- No context: It is unclear which module or function the message came from.
- Poor readability: In a production environment, logs are often parsed by machines, not humans.
First Step Towards Order: Log Levels
A best practice is to use standard levels:
debug— for debugging during development.info— for normal events (request processed, user authenticated).warn— for potential problems (low memory, deprecated API).error— for critical failures.
Example of a simple but already useful approach:
// bad.tsconsole.log('User logged in'); // ❌
// good.tsconst logger = { info: (message: string, meta?: Record<string, unknown>) => { console.log(JSON.stringify({ level: 'info', message, timestamp: new Date().toISOString(), ...meta })); }, error: (message: string, error?: Error) => { console.error(JSON.stringify({ level: 'error', message, stack: error?.stack, timestamp: new Date().toISOString() })); }};
logger.info('User logged in', { userId: 123 }); // ✅Here we add a timestamp and a level. This is already parseable. But writing such code manually for every project is inefficient — use ready-made libraries.
2. Choosing a Logging Library in TypeScript
There are several excellent libraries for TypeScript. Let's look at the two most popular: Winston and tslog. For beginners, we recommend tslog due to its simplicity and full TypeScript support.
tslog — The Ideal Choice for Beginners
This library is written in TypeScript and provides beautiful, colorful logs in the console. It does not require complex setup.
// Installation: npm install tslogimport { Logger } from 'tslog';
const logger = new Logger({ name: 'MyApp' });
logger.info('Application started');logger.warn('Memory is running low');logger.error('Database connection error', { retryCount: 3 });Advantages of tslog:
- Automatic TypeScript support (types included).
- Beautiful console output with colors.
- Built-in log levels.
- Minimal entry barrier.
Winston — Powerful but More Complex
Winston is an industry standard. It allows you to send logs to files, databases, Elasticsearch. However, for simple projects, its configuration can be overkill.
// Installation: npm install winstonimport winston from 'winston';
const logger = winston.createLogger({ level: 'info', format: winston.form