{"id":26488197,"url":"https://github.com/jeepway/logger-c","last_synced_at":"2025-03-20T06:58:42.142Z","repository":{"id":281370797,"uuid":"945075502","full_name":"JeepWay/logger-c","owner":"JeepWay","description":"A lightweight, flexible logger library for C language","archived":false,"fork":false,"pushed_at":"2025-03-11T16:45:50.000Z","size":337,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T06:58:32.369Z","etag":null,"topics":["c","c-language","logger","logging"],"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/JeepWay.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":"2025-03-08T15:51:04.000Z","updated_at":"2025-03-14T11:06:26.000Z","dependencies_parsed_at":"2025-03-08T16:41:53.487Z","dependency_job_id":null,"html_url":"https://github.com/JeepWay/logger-c","commit_stats":null,"previous_names":["jeepway/loggingc","jeepway/logger-c"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Flogger-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Flogger-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Flogger-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Flogger-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JeepWay","download_url":"https://codeload.github.com/JeepWay/logger-c/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244566880,"owners_count":20473451,"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","c-language","logger","logging"],"created_at":"2025-03-20T06:58:41.322Z","updated_at":"2025-03-20T06:58:42.130Z","avatar_url":"https://github.com/JeepWay.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Logger C\r\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) \r\n\r\n![root-default.png](fig/root-default.png)\r\n\r\nA lightweight, flexible logger library for the C language, inspired by [log4.c](https://github.com/rxi/log.c) project and Python's [logging](https://docs.python.org/3/library/logging.html) package. \r\nThis library provides a simple yet powerful way to log messages with different levels, handlers, and formatting options. It supports both stream and file handlers, and customizable log formats.\r\n\r\n## Features\r\n- Default root handler (a stream handler) for immediate use, you don't need to create it.\r\n- More file and stream handlers can be added.\r\n- Multiple log levels: `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`.\r\n- Customizable log formatting with color support for terminals.\r\n- Flexible date/time formatting.\r\n\r\n## Simple Usage\r\n1. Clone or download the repository:\r\n   ```bash\r\n   git clone https://github.com/JeepWay/logger-c.git\r\n    ```\r\n2. Include the header file [logger.h](logger/logger.h) and source file [logger.c](logger/logger.c) in your project. The following is an example (main.c): \r\n    ```cpp\r\n    #include \u003cstdio.h\u003e\r\n    #include \u003cstdlib.h\u003e\r\n    #include \"logger/logger.h\"\r\n    int main(void)\r\n    {\r\n        log_trace(\"Trace message %d\", 1);\r\n        log_debug(\"Debug message %d\", 2);\r\n        log_info(\"Info message %d\", 3);\r\n        log_warn(\"Warn message %d\", 4);\r\n        log_error(\"Error message %d\", 5);\r\n        log_fatal(\"Fatal message %d\", 6);\r\n        return 0;\r\n    }\r\n    ```\r\n3. Compile your program with a C compiler (e.g., gcc), and execute it.\r\n    ```bash\r\n    gcc -o main logger/logger.c main.c\r\n    ./main\r\n    ```\r\n4. You will see the result in the console like the top picture.\r\n\r\n\r\n## Basic Usage\r\nThe following `DEFAULT` values are defined in [logger.h](logger/logger.h#L26). You can use them to set the default values for convenience, or you can specify your own values.\r\n\r\n### 1. Change default log level\r\nThe default log level of root handler is `TRACE`. You can change the log level by using `log_set_level(char* name, size_t value)`, where `name` is the handler name and `value` is the log level.\r\n```cpp\r\nlog_set_level(DEFAULT, LOG_INFO);\r\n// Equal to the following line\r\nlog_set_level(\"root\", LOG_INFO); \r\n```\r\n![root-info.png](fig/root-info.png)\r\n\r\n### 2. Add a file handler\r\nThe default setting only contains a stream handler (root handler). You can add a file handler to log messages to a file by using `log_add_file_handler(char *filename, char *filemode, size_t level, char *name)`, where `filename` is the name of the file, `filemode` is the mode to open the file, `level` is the log level, and `name` (can't be `NULL`) is the handler name.\r\n```cpp\r\nlog_add_file_handler(DEFAULT, DEFAULT, DEFAULT_LEVEL, \"file1\");\r\n// Equal to the following line\r\nlog_add_file_handler(\"logger/program.log\", \"a\", LOG_INFO, \"file1\");\r\n```\r\nAnd then you can see the log messages in the file `logger/program.log`.\r\n\r\n![file-default.png](fig/file-default.png)\r\n\r\n### 3. Add a stream handler\r\nYou can add a stream handler to log messages to the console by using `log_add_stream_handler(FILE *fp, size_t level, char *name)`, where `fp` is the file pointer, `level` is the log level, and `name` (can't be `NULL`) is the handler name.\r\n```cpp\r\nlog_add_stream_handler(DEFAULT, DEFAULT_LEVEL, \"bash2\");\r\n// Equal to the following line\r\nlog_add_stream_handler(stderr, LOG_INFO, \"bash2\");\r\n```\r\nYou will see the log messages in the console. Since the log level of `bash2` is `INFO` and the log level of `root` is `TRACE`, only log messages with level `INFO` and above will appear twice in the console (once from each handler).\r\n\r\n![two-stream-handlers.png](fig/two-stream-handlers.png)\r\n\r\n### 4. Make a handler quiet\r\nEvery handler is not quiet by default. You can make a handler quiet by using `log_set_quiet(char* name, bool value)`, where `name` is the handler name and `value` is a boolean value.\r\n```cpp\r\nlog_set_quiet(\"root\", true);\r\nlog_add_stream_handler(DEFAULT, DEFAULT_LEVEL, \"bash2\");\r\n```\r\nThe console will only show the log messages from the `bash2` handler, as the `root` handler is quiet.\r\n\r\n![root-quiet.png](fig/root-quiet.png)\r\n\r\n### 5. Change data and time format\r\nYou can change the date and time format by using `log_set_date_fmt(char* name, char* date_fmt)`, where `name` is the handler name and `date_fmt` is the date and time format.\r\n```cpp\r\nlog_set_date_fmt(\"root\", DEFAULT_DATE_FORMAT3);\r\n// Equal to the following line\r\nlog_set_date_fmt(\"root\", \"%Y/%m/%d %H:%M:%S\");\r\n```\r\n![root-date3.png](fig/root-date3.png)\r\n\r\n### 6. Change text format\r\nThe default text format for stream is [color_fmt1](logger/logger.c#L201) function, for file is [no_color_fmt1](logger/logger.c#L215) function. \r\nThe avaliable text format function are `color_fmt1`, `color_fmt2`, `no_color_fmt1`, `no_color_fmt2`.\r\nYou can change the text format by using `log_set_fmt_fn(char* name, log_fmt_t fmt)`, where `name` is the handler name and `fmt` is the function pointer to the text format function.\r\n```cpp\r\nlog_set_fmt_fn(\"root\", color_fmt2);\r\n```\r\nYou will see the name of the handler in the log messages.\r\n\r\n![root-color2.png](fig/root-color2.png)\r\n\r\nYou can also change the text from colorful to plain. Note that the colorful format only works in the terminal and doesn't work in the text file.\r\n```cpp\r\nlog_set_fmt_fn(\"root\", no_color_fmt2);\r\n```\r\n![root-no-color2.png](fig/root-no-color2.png)\r\n\r\nIf you don't like the default text format, you can define your own text format function and use it. Below is an example of a custom text format function.\r\n```cpp\r\nvoid custom_fmt(record_t *rec, const char *time_buf)\r\n{\r\n    static const char *fmt = \"%s\\x1b[0m \\x1b[90m[%s:%d] %s%-5s:\\x1b[0m \";\r\n    fprintf(rec-\u003ehd_fp, fmt, time_buf, rec-\u003efile, rec-\u003eline, \r\n        level_colors[rec-\u003elevel], level_strings[rec-\u003elevel]);\r\n}\r\n\r\nlog_set_fmt_fn(\"root\", custom_fmt);\r\n```\r\n![root-custom-fmt.png](fig/root-custom-fmt.png)\r\n\r\n### 7. Change dump log function\r\nThe default dump log function is [dump_log](logger/logger.c#L190) function. \r\n```cpp\r\nvoid dump_log(record_t *rec)\r\n{\r\n    char time_buf[32];\r\n    time_buf[strftime(time_buf, sizeof(time_buf), rec-\u003ehd_date_fmt,\r\n                      rec-\u003etime)] = '\\0';\r\n    rec-\u003ehd_fmt_fn(rec, time_buf);\r\n    vfprintf(rec-\u003ehd_fp, rec-\u003emsg_fmt, rec-\u003eap);\r\n    fprintf(rec-\u003ehd_fp, \"\\n\");\r\n    fflush(rec-\u003ehd_fp);\r\n}\r\n```\r\nYou can change the dump log function by using `log_set_dump_fn(char* name, log_dump_fn dump_fn)`, where `name` is the handler name and `dump_fn` is the function pointer to the dump log function.\r\n\r\n\r\n## Examples\r\n```cpp\r\n#include \u003cstdio.h\u003e\r\n#include \u003cstdlib.h\u003e\r\n#include \"logger/logger.h\"\r\n\r\nint main(void)\r\n{\r\n    char *f1 = \"file1\";\r\n    log_add_file_handler(\"logger/program.log\", \"w\", LOG_WARN, f1);\r\n    log_set_fmt_fn(f1, no_color_fmt2);\r\n    log_set_quiet(\"root\", true);\r\n\r\n    log_trace(\"Trace message %d\", 1);\r\n    log_debug(\"Debug message %d\", 2);\r\n    log_info(\"Info message %d\", 3);\r\n    log_warn(\"Warn message %d\", 4);\r\n    log_error(\"Error message %d\", 5);\r\n    log_fatal(\"Fatal message %d\", 6);\r\n    return 0;\r\n}\r\n```\r\nYou will only see the log messages in the file `logger/program.log` with the log level `WARN` and above. Nothing will appear in the console.\r\n\r\n![example1.png](fig/example1.png)\r\n\r\n\r\n## Contributions\r\nContributions are welcome! Please submit a pull request or open an issue on the repository. Here are some ways you can contribute:\r\n* Implement a filter function, like the [filter class](https://docs.python.org/3/library/logging.html#filter-objects) in Python's logging package, to filter log messages based on the maximum log level and handler name.\r\n* Make the sequential search of handlers more efficient, for example, hashing the handler names into id.\r\n* Prevent adding handlers with the same name.\r\n* Add more safety checks in [logger.c](logger/logger.c#L175) to ensure robustness and prevent potential issues.\r\n\r\n\r\n## Reference\r\n* [log4.c](https://github.com/rxi/log.c)\r\n* [Python logging module](https://docs.python.org/3/library/logging.html) - Comprehensive guide on Python's logging package.\r\n* [Logging in Python](https://realpython.com/python-logging/) - An article to introduce the logging package.\r\n\r\n\r\n## License\r\nThis library is free. You can redistribute and modify it under the MIT License. See the [LICENSE](LICENSE) for details.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeepway%2Flogger-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeepway%2Flogger-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeepway%2Flogger-c/lists"}