2025-05-15 10:30:53 +08:00
2024-11-03 11:51:08 +08:00
2024-09-21 15:21:53 +08:00
2025-05-15 10:30:53 +08:00
2024-11-24 20:26:04 +08:00
2025-05-15 10:30:53 +08:00
2025-05-15 10:30:53 +08:00
2024-11-25 22:19:04 +08:00
2025-05-15 10:03:17 +08:00
2024-08-12 18:13:42 +08:00
2024-11-24 20:26:04 +08:00
2024-11-24 20:26:04 +08:00
2025-05-15 10:03:17 +08:00

C language logging library logging

brief

Logging is a lightweight and easy-to-use C language log library that supports log level, log format, log output, log files, and other functions.

function

  • Support log levels: DEBUG, INFO, Warning, ERROR, FATAL
  • Support log formats: timestamp, log level, log content
  • Support log output: console, file
  • Support log files: automatic creation, automatic scrolling, log segmentation

install

  • Conan
conan create .
  • cmake
git clone https://github.com/WangZhongDian/logging.git
cd logging
cmake build -B build . && cd build && cmake --build .
cmake --install .

usage

console log

#include "logging.h"

int main() {
    Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG);

    log_info("This is an info message");
    log_error("This is an error message%s", "123");
    log_fatal("This is an fatal message");
    log_debug("This is a debug message");
    log_warning("This is a warning message%s", "123");

    destroyDefaultLogger();
    return 0;
}

file log

#include "logging.h"
#include "logging/logging-handler.h"

int main() {
    Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG);
    logger->addHandler(loggingHandlerFile("test1", 1024*1024));

    log_info("This is an info message");
    log_error("This is an error message%s", "123");
    log_fatal("This is an fatal message");
    log_debug("This is a debug message");
    log_warning("This is a warning message%s", "123");

    destroyDefaultLogger();
    return 0;
}

Logging filter

Support adding custom filters, currently with built-in substring filters The function of an filter is to redirect filtered logs to the filter's dedicated processor

example

Redirects filtered logs to a dedicated file processor

#include "logging.h"
#include <stdio.h>

int main() {
    Logger  *logger = newDefaultLogger("testLogger", LOG_DEBUG);

    log_info("This is an info message");
    log_error("This is an error message%s", "123");
    log_fatal("This is an fatal message");
    log_debug("This is a debug message");
    log_warning("This is a warning message%s", "123");

    char *test1[]         = {"123", "tt", NULL};

    log_filter *tint = loggingFilterSubStr(
        test1,
        LOG_DEBUG,
        loggingHandlerFile("test_interceptor", 1024 * 1024),
        true);

    logger->addFilter(tint);

    printf("\n");
    printf("filter added\n");
    printf("\n");

    log_info("This is an info message");
    log_error("This is an error message%s", "123");
    log_fatal("This is an fatal message");
    log_debug("This is a debug message");
    log_warning("This is a warning message%s", "123");

    destroyDefaultLogger();
    return 0;
}

Multiple substring filters

#include "logging.h"
#include <stdbool.h>
#include <stdio.h>
#include <time.h>

int main() {
    Logger *logger = newDefaultLogger("testLogger", LOG_DEBUG);

    log_info("This is an info message");
    log_error("This is an error message%s", "123");
    log_fatal("This is an fatal message");
    log_debug("This is a debug message");
    log_warning("This is a warning message%s", "123");

    char *test1[]         = {"This",NULL};

    log_filter *tint = loggingFilterSubStr(
        test1,
        LOG_DEBUG,
        loggingHandlerFile("test_interceptor", 1024 * 1024),
        false);

    logger->addFilter(tint);

    char *test2[]         = {"123",NULL};

    log_filter *tint1 = loggingFilterSubStr(
        test2,
        LOG_DEBUG,
        loggingHandlerFile("test_interceptor1", 1024 * 1024),
        true);

    logger->addFilter(tint1);

    printf("\n");
    printf("filter added\n");
    printf("\n");

    log_info("This is an info message");
    log_error("This is an error message%s", "123");
    log_fatal("This is an fatal message");
    log_debug("This is a debug message");
    log_warning("This is a warning message%s", "123");

    destroyDefaultLogger();
    return 0;
}
Description
No description provided
Readme MIT 465 KiB
v1.0.0 Latest
2025-10-18 09:06:21 +08:00
Languages
C 84.4%
CMake 7.2%
Python 7%
Shell 0.7%
Makefile 0.4%
Other 0.3%