AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
AnCH logging library documentation

Introduction

AnCH logging library provides configurable loggers which write messages through writers (console, files with rotation), with a map diagnostic context.

Prerequisites

  • anch-conf: logging configuration is read in the anch::log section of the application configuration
  • anch-date: high precision dates in messages
  • anch-utils: singletons

Installation

CMake target anch::log:

find_package(Anch REQUIRED)
target_link_libraries(my-app PRIVATE anch::log)

See installation.

Usage

Loggers are retrieved with LoggerFactory::getLogger once the configuration has been loaded. Logger names are hierarchical (org::myapp::Main ): the most specific configured logger applies (org::myapp , then org , then the default logger).

Messages are logged with level methods (trace , debug , info , warn , error , fatal ), which concatenate their arguments, or with stream injection (log/stream.hpp ):

With both APIs, messages in levels lower than ANCH_LOG_MIN_LEVEL CMake option (TRACE by default) are removed at compile time. Arguments are always evaluated, even when the level is disabled. Logging never throws.

Configuration

  • anch::log.thread-safe : writers are protected against concurrent access (default true ).
  • anch::log.low-priority : writers write messages in a dedicated thread (default false ).
  • [anch::log.writer.<name>] : writer declaration:
    • module : console or file ;
    • pattern : message pattern: $c logger name, $p level, $m message, $t thread identifier, $d{strftime format} date, $D{anch-date format} high precision date, $C{key} MDC value ;
    • path , max-size (with K , M , G or T unit) and max-rotate-files for file writers ;
    • thread-safe and low-priority override the global values.
  • [anch::log.logger] : default logger ; [anch::log.logger.<name>] : logger for a name prefix:
    • writers : comma separated writer names ;
    • level : trace , debug , info , warn , error or fatal .

Without anch::log section, messages are written on the console.

Example

application.ini :

[anch::log.writer.console]
module=console
pattern=$d{%Y-%m-%d %H:%M:%S} - $c: $p - $m
[anch::log.writer.file]
module=file
path=./application.log
pattern=$d{%Y-%m-%d %H:%M:%S} [$t] $p $c - $m
max-size=10M
max-rotate-files=5
[anch::log.logger]
writers=console
level=info
[anch::log.logger.org::myapp]
writers=console,file
level=debug
#include "conf/configuration.hpp"
#include "log/loggerFactory.hpp"
#include "log/stream.hpp"
int
main() {
anch::conf::Configuration::loader().name("application").load(); // application.ini (anch::log section)
const anch::log::Logger& LOG = anch::log::LoggerFactory::getLogger("org::myapp::Main");
LOG.info("Application started with ", 3, " workers"); // variadic API
int value = 42;
LOG << anch::log::lvl::debug << "value=" << value; // stream API
return 0;
}

Console output:

2026-11-12 21:35:07 - org::myapp::Main: INFO - Application started with 3 workers
2026-11-12 21:35:07 - org::myapp::Main: DEBUG - value=42, ratio=0.5