Commonly Used Logging Library Zlog in Embedded Linux Projects

Zlog is a high-performance, thread-safe, flexible, and conceptually clear pure C logging function library.

Commonly Used Logging Library Zlog in Embedded Linux Projects

Follow “Programmers Love Learning“, set “Starred Public Account

Useful Resources Delivered First Hand!

1. Installation

Download the zlog-1.2.17.tar.gz file from https://github.com/HardySimpson/zlog/releases/tag/1.2.17

Unzip and install

$ tar -zxvf zlog-1.2.17.tar.gz
$ cd zlog-1.2.17/
$ ./configure --enable-test # Compile the test program and examples, just for the sake of it
$ make
$ sudo make install
# By default, it is installed under /usr/local/, including libzlog.so and zlog.h

2. Introduction to the Configuration File

There are three important concepts in zlog: category, format, and rule.

  • Category is used to differentiate different inputs. The name of the category variable in the code is a string. In a program, you can use the category name to output logs of different categories for different purposes.

  • Format describes the format of the output logs, such as whether it includes a timestamp or file location information. In the previous example, the simple format is configured to be a simple user input message followed by a newline character.

  • Rule combines the category, level, output file, and format to decide whether a log in the code will be output, where it will be output, and in what format. In simple terms, if the category string in the rule matches the name of the category variable in the code, it will match. There are also more advanced hierarchical category matching options. The rule completely decouples the strong binding between various elements. For example, log4j requires specifying a level for each category (or inheriting from a parent category), which is very inconvenient when multiple layers of systems need their own level requirements.

Now try to write a configuration file. The name of the configuration file doesn’t matter, and its location doesn’t matter either; it can be specified when calling zlog_init().

$ cat /etc/zlog.conf

[formats]
simple = "%m%n"
[rules]
my_cat.DEBUG >stdout; simple

In the current example of the configuration file, you can see that logs of the my_cat category with a level of >=debug will be output to stdout (standard output), and the output format is the simple format, which is the user input message followed by a newline character. If you want to output to a file and control the file size to 1MB, the rule configuration should be:

my_cat.DEBUG            "/var/log/aa.log", 1M; simple

3. Using in Code

The code for test_hello.c is as follows:

#include <stdio.h> 

#include "zlog.h"

int main(int argc, char** argv)
{
    int rc;
    zlog_category_t *c;

    rc = zlog_init("/etc/zlog.conf");
    if (rc) {
        printf("init failed\n");
        return -1;
    }

    c = zlog_get_category("my_cat");
    if (!c) {
        printf("get cat fail\n");
        zlog_fini();
        return -2;
    }

    ZLOG_INFO(c, "hello, zlog");

    zlog_fini();

    return 0;
}

4. Compile and Run!

$ gcc -c -o test_hello.o test_hello.c -I/usr/local/include
$ gcc -o test_hello test_hello.o -L/usr/local/lib -lzlog
$ ./test_hello
hello, zlog

5. Advanced Features

  • Syslog-style configuration file, easy to learn and use.
  • Flexible configuration of log output format, similar to log4j’s pattern layout.
  • Hierarchical category model, clearer than log4j’s inheritance model.
  • Multiple outputs, including dynamic files, static files, stdout, stderr, and syslog.
  • Dynamic refresh of configuration at runtime by simply calling the function zlog_reload().
  • High performance, achieving 72,000 logs per second on my laptop, approximately 200 times the speed of syslog(3) with rsyslogd.
  • Users can define their own levels without changing the library code.
  • Safe log rotation when multiple threads and processes write to the same log (log rotation is renaming oversized logs).
  • Precision down to microseconds.
  • If a program uses only one category for output, zlog provides a simple call wrapper dzlog.
  • MDC, a log4j-style key-value pair table, can extend user-defined fields.
  • Self-diagnosis, can output zlog’s own logs and configuration status at runtime.
  • No dependency on other libraries, as long as it is a POSIX system.

Recommended Articles

Embedded Basics – Testing Basic Concepts
Embedded Basics – RSA Asymmetric Encryption Principles
Embedded Basics – IP Address and Subnet Partitioning
Embedded Basics – Information Security and Encryption

Leave a Comment