Sobes.tech
Junior — Middle

How is data collected and stored during the logging process?

sobes.tech AI

Answer from AI

The logging process involves several stages: data collection, formatting, and storage (output).

  1. Data collection — obtaining information about the event to be logged. Usually, this includes a message, log level (e.g., INFO, ERROR), event time, and context (e.g., class or method name).

  2. Formatting — converting data into a human-readable or structured format (text, JSON, etc.).

  3. Storage/output — writing logs to a target medium: a file, console, remote server, or database.

In Java, libraries such as Log4j, SLF4J, and java.util.logging are often used for logging, implementing this process. When the logging method is called, data is passed to the logger, which creates an entry and sends it to appenders (handlers) responsible for saving.

Example using SLF4J:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Example {
    private static final Logger logger = LoggerFactory.getLogger(Example.class);

    public void process() {
        logger.info("Starting process");
        try {
            // code
        } catch (Exception e) {
            logger.error("Error during process", e);
        }
    }
}

Here, when logger.info or logger.error is called, data is collected and passed to the logging system, which saves it according to the configuration.