{"id":21688430,"url":"https://github.com/emomaxd/flog","last_synced_at":"2025-03-20T12:27:19.838Z","repository":{"id":264168554,"uuid":"892088521","full_name":"emomaxd/flog","owner":"emomaxd","description":"header-only logging library","archived":false,"fork":false,"pushed_at":"2024-11-22T11:30:51.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-25T12:41:26.198Z","etag":null,"topics":["c-plus-plus","data","files","formatting","logging","stdout"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emomaxd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-21T13:44:25.000Z","updated_at":"2024-12-07T13:19:10.000Z","dependencies_parsed_at":"2024-11-22T12:23:15.116Z","dependency_job_id":"094d828d-6148-4b07-8ef2-4cc41b84af9f","html_url":"https://github.com/emomaxd/flog","commit_stats":null,"previous_names":["emomaxd/flog"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emomaxd%2Fflog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emomaxd%2Fflog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emomaxd%2Fflog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emomaxd%2Fflog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emomaxd","download_url":"https://codeload.github.com/emomaxd/flog/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244610650,"owners_count":20481031,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-plus-plus","data","files","formatting","logging","stdout"],"created_at":"2024-11-25T17:14:59.845Z","updated_at":"2025-03-20T12:27:19.789Z","avatar_url":"https://github.com/emomaxd.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# flog - C++ Logging Library\n\n**flog** is a simple and efficient C++ logging library that supports basic logging and formatting with the help of [ff](https://github.com/emomaxd/ff) library, see below.\n\nFeatures:\n---------\n- **Log Levels**: TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL.\n- **Color-Coded Output**: Different log levels are colored for easier reading in the console.\n- **Async Logging**: Can offload logging to a thread pool for non-blocking log writes.\n- **Custom File Logging**: Ability to configure log file path and log rotation parameters.\n- **Log Rotation**: Automatically rotates log files when they exceed a specified size.\n\nUsage\n-----\nTo use the flog logging system, you need to include the \"flog.h\" header file and follow the steps below.\n\n0. **Use default logger**\n    ```cpp\n    // Logging with default logger\n    flog::trace(\"Scientific notation: {s}\", 123456.789);  // Logs in scientific notation\n    flog::debug(\"Formatted float: {.3}\", 3.14159);  // Logs float with 3 decimal places\n    flog::info(\"Binary format with positional parameter: {1:b}\", 255, 12);  // Logs in binary format\n    flog::warn(\"Hex format: {x}\", 255);  // Logs in hex format (lowercase)\n    flog::trace(\"Hex format: {X}\", 255);  // Logs in hex format (uppercase)\n    ```\n\n1. **Create a Logger**:\n   The Logger class is the central component. You can create a Logger instance using the LoggerManager.\n\n   Example:\n   ```cpp\n   auto logger = flog::LoggerManager::createLogger(\"MyLogger\", std::cout);\n   ```\n\n2. **Log Messages**:\n   Instead of using the default logger you can use the logger you have created.\n\n   Example:\n   ```cpp\n    logger.trace(\"Trace level log.\");\n    logger.debug(\"Debug level log with number: {}\", 42);\n   ```\n\n3. **Asynchronous Logging**:\n   You can enable asynchronous logging by setting up a thread pool. Async logging offloads log writes to background threads for non-blocking performance.\n\n   Example:\n   ```cpp\n    // Enable async logging\n    flog::ThreadPool pool(4);  // 4 threads in the thread pool\n    logger-\u003eenableAsync(pool);\n   ```\n\n4. **Log Rotation**:\n   The log files will rotate automatically when the size of the log file exceeds the configured limit. The system keeps a specified number of backup files.\n\n   Example:\n   ```cpp\n   logger-\u003eenableFileLogging(\"log.txt\", 1024 * 1024); //(Optional) Rotate after 1MB\n   ```\n\n5. **Flushing Logs**:\n   You can flush the log output (both console and file) manually using the `flush` method.\n\n   Example:\n   ```cpp\n   logger-\u003eflush();\n   ```\n\nExample Usage:\n--------------\n```cpp\n#include \"flog.h\"\n\nint main() {\n    // Create a ThreadPool for async logging\n    flog::ThreadPool pool(4);  // 4 threads for async logging\n\n    // Create a logger with default output stream (std::cout)\n    flog::Logger logger(\"ExampleLogger\", std::cout);\n    \n    // Enable async logging\n    logger.enableAsync(pool);\n    \n    // Enable file logging\n    logger.enableFileLogging(\"log.txt\", 5 * 1024 * 1024); //(Optional) File rotating - rotate after 5MB\n    \n    // Set periodic flush (every 3 seconds)\n    logger.setPeriodicFlush(std::chrono::seconds(3));\n    \n    // Set the backtrace threshold for flushing the log buffer\n    logger.setBacktraceThreshold(5);\n    \n    // Logging some messages with various levels\n    logger.trace(\"Trace level log.\");\n    logger.debug(\"Debug level log with number: {}\", 42);\n    logger.info(\"Info level log: {0} is the current value of {1}\", 13, 10);\n    logger.warn(\"Warning log: Low disk space.\");\n    logger.error(\"Error log: Failed to load configuration.\");\n    logger.critical(\"Critical log: System failure imminent!\");\n    \n    // Demonstrating file logging and rotation\n    logger.info(\"This will be logged to the file.\");\n    for (int i = 0; i \u003c 1000; ++i) {\n        logger.info(\"Logging to file: {}\", i);\n        std::this_thread::sleep_for(std::chrono::milliseconds(10));\n    }\n\n    // Test logging with scientific, binary, hex formatting\n    double pi = 3.141592653589793;\n    int number = 255;\n    logger.info(\"Scientific notation: {.3}\", pi);\n    logger.info(\"Binary format: {b}\", number);\n    logger.info(\"Hexadecimal format: {X}\", number);\n\n    // Demonstrating the default static logger\n    flog::trace(\"Using default logger trace.\");\n    flog::debug(\"Using default logger debug with value: {}\", 123);\n    //flog::info(\"Using default logger info: {}\", \"message\");\n\n    // Allow async tasks to complete\n    std::this_thread::sleep_for(std::chrono::seconds(5));\n\n    // Shutdown the logger and thread pool\n    flog::LoggerManager::shutdown();\n\n    return 0;\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femomaxd%2Fflog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femomaxd%2Fflog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femomaxd%2Fflog/lists"}