{"id":20102261,"url":"https://github.com/temikfart/logger","last_synced_at":"2026-01-07T12:04:37.280Z","repository":{"id":62777891,"uuid":"540028080","full_name":"temikfart/logger","owner":"temikfart","description":"Simple logger for personal use","archived":false,"fork":false,"pushed_at":"2024-06-01T13:40:14.000Z","size":30,"stargazers_count":1,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T02:28:59.285Z","etag":null,"topics":["cpp","log","logger","logging","logs"],"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/temikfart.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":"2022-09-22T14:42:24.000Z","updated_at":"2024-06-01T13:40:17.000Z","dependencies_parsed_at":"2023-02-09T15:16:28.595Z","dependency_job_id":"0facc9d8-aebb-4b11-b4a1-9916bbade85e","html_url":"https://github.com/temikfart/logger","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/temikfart%2Flogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/temikfart%2Flogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/temikfart%2Flogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/temikfart%2Flogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/temikfart","download_url":"https://codeload.github.com/temikfart/logger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245949555,"owners_count":20698917,"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":["cpp","log","logger","logging","logs"],"created_at":"2024-11-13T17:29:28.804Z","updated_at":"2026-01-07T12:04:37.208Z","avatar_url":"https://github.com/temikfart.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Logger\n\n---\nIt is an attempt to create simple, readable, flexible\nand universal logger for personal purposes.\nThe main idea is implement useful tool,\nwhich can be easy added to any `C++` project.\n\nFirst ideas, which were implemented here,\nwere come up and developed in the \n[SQL to CypherQL Converter](https://github.com/temikfart/sql2cypher.git).\n\n### Table of contents\n\n1. [How to use](#How-to-use)\n2. [Appenders](#Appenders)\n   1. [FileAppender](#FileAppender)\n   2. [ConsoleAppender](#ConsoleAppender)\n3. [Formatters](#Formatters)\n   1. [TXTFormatter](#TXTFormatter)\n   2. [FuncMessagesFormatter](#FuncMessagesFormatter)\n   3. [OnlyMessagesFormatter](#OnlyMessagesFormatter)\n   4. [JSONFormatter](#JSONFormatter)\n4. [Inspired by](#Inspired-by)\n5. [License](#License)\n\n## How to use\n\nNow, you can download this repository and manually integrate\n`Logger` into your project.\n* Add `logger/include` to the project include paths;\n* Import `logger/log.hpp`, where you want to use `Logger`;\n* Import header with special initializer like `file_appender_initializer.hpp` to init the logger (only once);\n* Write your first log:\n    ```C++\n    #include \"logger/log.hpp\"\n    #include \"logger/initializers/file_appender_initializer.hpp\"\n    \n    int main() {\n        logger::init(logger::info, \"../log\");\n  \n        LOG(logger::info) \u003c\u003c \"Hello, world!\";\n        LOGI \u003c\u003c \"Short form\";\n        \n        return 0;\n    }\n    ```\n* Read logs in the `2022-09-25-22:26:16.log` file:\n    ```\n    2022-09-25 22:26:16 [INFO] main.cpp main() at line 7: Hello, world!\n    2022-09-25 22:26:16 [INFO] main.cpp main() at line 8: Short form\n    ```\n\n## Macros\n\nThis tools contains a lot of macros.\nYou can choose any _(See file `log.hpp`)_.\n\nThe most important and \"root\" macros are `LOG(severity)` and\n`LOG_IF(condition, severity)`.\n\n| Original macro     | Long form | Short form | Message       |\n|--------------------|-----------|------------|---------------|\n| LOG(logger::fatal) | LOG_FATAL | LOGF       | `fatal` error |\n| LOG(logger::error) | LOG_ERROR | LOGE       | `error`       |\n| LOG(logger::warn)  | LOG_WARN  | LOGW       | `warn`        |\n| LOG(logger::info)  | LOG_INFO  | LOGI       | `info`        |\n| LOG(logger::trace) | LOG_TRACE | LOGT       | `trace`       |\n| LOG(logger::debug) | LOG_DEBUG | LOGD       | `debug`       |\n\nThe same table of macros, but with condition:\n\n| Original macro                   | Long form               | Short form         | Message       |\n|----------------------------------|-------------------------|--------------------|---------------|\n| LOG_IF(condition, logger::fatal) | LOG_FATAL_IF(condition) | LOGF_IF(condition) | `fatal` error |\n| LOG_IF(condition, logger::error) | LOG_ERROR_IF(condition) | LOGE_IF(condition) | `error`       |\n| LOG_IF(condition, logger::warn)  | LOG_WARN_IF(condition)  | LOGW_IF(condition) | `warn`        |\n| LOG_IF(condition, logger::info)  | LOG_INFO_IF(condition)  | LOGI_IF(condition) | `info`        |\n| LOG_IF(condition, logger::trace) | LOG_TRACE_IF(condition) | LOGT_IF(condition) | `trace`       |\n| LOG_IF(condition, logger::debug) | LOG_DEBUG_IF(condition) | LOGD_IF(condition) | `debug`       |\n\n## Appenders\nYou can write logs in the several places at the same time.\nJust add new appender to the `logger` instance:\n```C++\nlogger::init(logger::debug, \"../log\");\nlogger::init(logger::error, logger::cerr);\n\nLOGF \u003c\u003c \"fatal error\";\n```\nLog _\"fatal error\"_ will be written in the file inside the `../log`\ndirectory and in the `cerr` stream too.\n\n_Notice: if you set different severity levels to appenders,\nyou will have logs in the output stream according to message's\nseverity and severity of each appender._\n\n#### Example:\n```C++\nlogger::init(logger::debug, \"../log\");\nlogger::init(logger::error, logger::cerr);\n\nLOGE \u003c\u003c \"Something went wrong!\"; // will be written in both\n// ...\nLOGD \u003c\u003c \"try to open file...\";   // will be written only in the file\nopen(file);\n```\n\n### FileAppender\n\nThis appender writes logs in the special `.log` file,\npath of which was specified during `logger::init` call.\n\n#### File Creation\n\nWhen you invoke `logger::init()`, you pass path to the log's directory\nor to the desired regular file as the second argument.\nThis path is processed by internal functions and as result \nnew folder for the logs will be created.\n\nNew folder path will be according to this table:\n\n| Existence         | Your path                  | Result folder      | Result path     |\n|-------------------|----------------------------|--------------------|-----------------|\n| _[exists]_        | `/path/dir/file.cpp`       | _[parent path]_    | `/path/dir`     |\n| _[exists]_        | `/path/dir`                | _[current path]_   | `/path/dir`     |\n| _[doesn't exist]_ | `/path/dir/not_exists.cpp` | _[parent path]_    | `/path/dir`     |\n| _[doesn't exist]_ | `/path/dir`                | _[current path]_   | `/path/dir`     |\n| _[doesn't exist]_ | `/path/dir/`               | _[path to subdir]_ | `/path/dir/dir` |\n\n### ConsoleAppender\n\nThis appender writes logs in the console stream (`stdout` or `stderr`),\nwhich was specified during `logger::init` call.\nLogs recorded with the `ConsoleAppender` have special colours \naccording to the `Severity` level.\n\nYou can colour every message with special severity as you want.\nJust use method `set_console_colour` in the `Logger` class:\n```C++\nvoid set_console_colour(Severity severity, const MessageColours\u0026 msg_cols);\n```\nwhere `MessageColours` is a struct, which consist of two colours:\nfor the text and its background.\n```C++\nstruct MessageColours {\n    Colour text;\n    Colour bg;\n};\n```\n\nAlso, you can turn coloured output off, just use the following methods:\n```C++\nvoid turn_colours_on();\nvoid turn_colours_off();\n```\n\n#### Example:\n```C++\nlogger::Logger::get()-\u003eset_console_colour(logger::info,\n                                          {logger::yellow, logger::common});\n```\nwhere `logger::common` equals to \"without colour\"\n(transparent background or common text colour).\n\n## Formatters\n\nEach formatter is a class, which has special static method `format`:\n```C++\nstatic std::string format(const Record\u0026 r);\n```\nThe Formatters can represent log records in different forms.\n\n### TXTFormatter\n\nWhen you use `init` function to initializing the logger this formatter\nis set by default. Log messages represents in the following format:\n```\n2022-09-25 22:26:16 [INFO] main.cpp main() at line 11: Hello, world!\n```\nas you can see, there are timestamp, severity, file, function, line and message.\n\n### FuncMessagesFormatter\n\nThis formatter contains only message with the function, which sent it, and line.\n```\nmain.cpp@11: Hello, world!\n```\n\n### OnlyMessagesFormatter\n\nYou can use this formatter, if you want see only the log messages.\n```\nHello, world!\n```\n\n### JSONFormatter\n\nThis formatter represents logs as json array:\n```json\n[\n\t{\n\t\t\"timestamp\":\"2023-02-17T12:46:31.237Z\",\n\t\t\"severity\":\"FATAL\",\n\t\t\"file\":\"main.cpp\",\n\t\t\"function\":\"main\",\n\t\t\"line\":\"9\",\n\t\t\"message\":\"Fatal\"\n\t},\n\t{\n\t\t\"timestamp\":\"2023-02-17T12:46:31.237Z\",\n\t\t\"severity\":\"ERROR\",\n\t\t\"file\":\"main.cpp\",\n\t\t\"function\":\"main\",\n\t\t\"line\":\"10\",\n\t\t\"message\":\"Error\"\n\t}\n]\n```\n\n## Inspired by\n\nSome ideas of implementation was inspired by\n[PLOG](https://github.com/SergiusTheBest/plog) project, which\nis more complicated and thoughtful for now :)\n\n## License\n\nThis project is licensed under the\n[MIT license](https://choosealicense.com/licenses/mit/).\nYou can freely use `Logger` in your commercial or opensource software.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftemikfart%2Flogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftemikfart%2Flogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftemikfart%2Flogger/lists"}