Logging

This document outlines logging conventions for developers working on the Urapolku Project. It introduces conventions for when to use specific log levels, as well as which tools we'll be using.

Introduction

This document outlines the logging conventions to be followed by developers working on our Urapolku Next.js application. Utilizing the Pino logging library, adhering to these conventions will ensure a consistent approach to logging, making it easier to monitor, debug, and maintain the application.

Pino Logging Library

Pino is a fast, low-overhead, and minimalistic logging library designed for server-side applications. In our Next.js application, we will use Pino to facilitate logging at various levels.

Logflare

In addition to writing logs out to the console, we will be collecting our logs using a logging platform called Logflare. This logging platform is responsible for keeping us notified and aware of the logging that happens in production. So in case something goes wrong, or in case we are above a certain warn log treshold, we should be able to be notified about this in order to respond to things before they break.

Logflare has integrations with our Vercel deployment platform.

Utility Functions

We will be adding utility functions in order to create the following log entries:

[00:00:00:0000 23/10/2023] INFO (Dashboard): The user Adem logged into the dashboard!

Where INFO is going to be the loglevel that will be color coded.

[00:00:00:0000 23/10/2023] is going to be the time it happened and also color coded.

(Dashboard) is going to be the tag associated with this log, and will also be color coded.

: is going to be the message that is going to be displayed.

Log Levels

The utility functions supports various log levels. Each level has a specific purpose and should be used accordingly. Here are the log levels and the conventions associated with each:

1. TRACE

Purpose:

  • To provide highly detailed logging information, useful for diagnosing and tracing issues at a granular level.

When to Use:

  • During development when fine-grained diagnostic information is necessary.

  • To trace the flow through the application or the flow of interactions with external systems.

Example:

const logger = require('pino')();
logger.trace('Entering function XYZ with arguments: ', args);

2. DEBUG

Purpose:

  • To provide detailed information on the application's operational flow.

When to Use:

  • During development and debugging.

  • When detailed information is required to diagnose issues.

Example:

const logger = require('pino')();
logger.debug('Fetching user data for user: ', userId);

3. INFO

Purpose:

  • To log general events or informational messages that highlight the application's progress.

When to Use:

  • To log key business transactions.

  • To record important system or application events.

Example:

const logger = require('pino')();
logger.info('User registration successful for user: ', userId);

4. WARN

Purpose:

  • To log unexpected events or conditions that may potentially cause an issue.

When to Use:

  • When there are non-critical issues that should be flagged.

  • To indicate a misuse of APIs or a potential problem that doesn’t cause the program to stop.

Example:

const logger = require('pino')();
logger.warn('Failed login attempt detected for user: ', userId);

5. ERROR

Purpose:

  • To log errors that prevent the application from proceeding.

When to Use:

  • When an error occurs that stops an operation but not the application.

  • To log exceptions or error conditions.

Example:

const logger = require('pino')();
logger.error('Database connection failed: ', errorDetails);

6. FATAL

Purpose:

  • To log severe errors that cause the application to terminate.

When to Use:

  • In situations where a critical system error occurs that prevents the application from continuing.

Example:

const logger = require('pino')();
logger.fatal('Critical system failure: ', errorDetails);

Conclusion

Following the conventions outlined in this document with the Pino logging library will ensure a unified approach to logging across the development team. This, in turn, will facilitate easier monitoring, debugging, and maintenance of the application.

Last updated

Was this helpful?