DEV Community

How to configure logback as json format

tonybui1812 on October 30, 2023

To configure Logback to output logs in JSON format, you can use the Logstash encoder, which formats log events as JSON objects. Here's how to confi...
Collapse
 
easytony profile image
tonybui1812

Why they use level value instead of level code?

Collapse
 
easytony profile image
tonybui1812

Logging frameworks often include both a textual representation of the log level (e.g., "INFO," "ERROR") and a numerical representation called the "level value" (e.g., 20000 for INFO, 40000 for ERROR). There are a few reasons why the level value is used alongside the level:

  1. Efficiency: Internally, it's more efficient for logging frameworks to compare and evaluate numerical values than text strings. When determining whether a log message should be logged based on its log level, comparing integers is faster than comparing strings.

  2. Consistency: Using numerical values ensures consistency in log level comparisons. Different programming languages and libraries may have variations in the textual representation of log levels (e.g., "INFO" vs. "Info" vs. "info"). Numerical values provide a standardized way to represent log levels across different systems.

  3. Custom Log Levels: Some logging frameworks allow users to define custom log levels with custom level values. Using numerical values allows for the flexibility of adding custom log levels without relying on predefined textual representations.

  4. Reduced Memory Consumption: Storing log levels as numerical values consumes less memory compared to storing them as strings, which can be important in systems with a high volume of log messages.

While log level values are primarily used internally by the logging framework, the textual log level representation (e.g., "INFO") remains essential for developers and operators to understand log messages easily. Typically, log aggregation and visualization tools, like Kibana, provide user-friendly representations of log levels based on the textual log level, making it easier for humans to interpret log data.

In summary, the inclusion of both level values and textual log levels in log messages is a design choice that balances efficiency and consistency in log level handling while maintaining readability for developers and operators.