Skip to content

ir/simple_logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

Logger Library

Provides a singleton logger instance that writes logs to a file and optionally prints error messages to std::cerr. It also manages log files by automatically creating a log directory, rotating logs, and keeping only the most recent 10 log files.


Features

  • Thread-Safe Logging: Uses std::mutex to ensure thread safety.
  • Log Levels: Supports INFO, WARNING, and ERROR log levels.
  • File Logging: Logs are written to a file in the APPDATA/brasilhook/logs directory (or ./logs if APPDATA is unavailable).
  • Log Rotation: Automatically keeps only the 10 most recent log files.
  • Timestamped Logs: Each log entry includes a timestamp for easy debugging.
  • Error Output: ERROR logs are also printed to std::cerr for immediate visibility.

Requirements

  • C++17 or later.
  • A compiler with support for the <filesystem> library (e.g., GCC 8+, Clang 7+, MSVC 2017+).

Usage

Include the Header

#include "logger.h"

Then use the provided methods to log messages at different levels:

logger::get_instance().info("This is an informational message.");
logger::get_instance().warning("This is a warning message.");
logger::get_instance().error("This is an error message.");

Example Program

#include "logger.h"
#include <thread>

void worker_thread() {
    for (int i = 0; i < 3; ++i) {
        logger::get_instance().info("Thread is running...");
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

int main() {
    logger::get_instance().info("Application started.");

    std::thread t1(worker_thread);
    std::thread t2(worker_thread);

    t1.join();
    t2.join();

    logger::get_instance().info("Application finished.");
    return 0;
}

Example Output

Log File (log_20231025_143022.txt)

[2023-10-25 14:30:22] [INFO] Application started.
[2023-10-25 14:30:22] [INFO] Thread is running...
[2023-10-25 14:30:23] [INFO] Thread is running...
[2023-10-25 14:30:24] [INFO] Thread is running...
[2023-10-25 14:30:25] [INFO] Application finished.

Console Output (for ERROR logs)

[2023-10-25 14:30:22] [ERROR] This is an error message.

Log Files

Each log file is named with a timestamp in the format:

log_YYYYMMDD_HHMMSS.txt

The logger automatically manages log files:

  • Only the 10 most recent log files are kept.
  • Older log files are deleted to save disk space.

API Reference

Singleton Access

Returns the singleton instance of the logger.

logger& logger::get_instance()

Logs an informational message.

Logging Methods

void logger::info(const std::string& message)

Logs a warning message.

void logger::warning(const std::string& message)

Logs an error message and prints it to std::cerr.

void logger::error(const std::string& message)

Internal Methods (Private)

  • std::string current_time(): Returns the current time in YYYY-MM-DD HH:MM:SS format.
  • std::string get_timestamp(): Returns a timestamp in YYYYMMDD_HHMMSS format.
  • std::string get_log_directory(): Returns the path to the log directory.
  • void create_log_directory(): Creates the log directory if it doesn't exist.
  • void manage_log_files(): Manages log file rotation.

About

Simple logger library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages