{"id":13495360,"url":"https://github.com/idelsink/b-log","last_synced_at":"2025-05-07T17:20:58.986Z","repository":{"id":54588224,"uuid":"62347699","full_name":"idelsink/b-log","owner":"idelsink","description":"Library for custom logging in bash called 'b-log'.","archived":false,"fork":false,"pushed_at":"2021-02-09T00:05:13.000Z","size":85,"stargazers_count":81,"open_issues_count":6,"forks_count":28,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T16:39:22.705Z","etag":null,"topics":["bash","file","library","logger","logging","stdout","syslog"],"latest_commit_sha":null,"homepage":"https://github.com/idelsink/b-log","language":"Shell","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/idelsink.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}},"created_at":"2016-06-30T23:10:16.000Z","updated_at":"2025-02-11T17:04:25.000Z","dependencies_parsed_at":"2022-08-13T20:40:17.371Z","dependency_job_id":null,"html_url":"https://github.com/idelsink/b-log","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idelsink%2Fb-log","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idelsink%2Fb-log/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idelsink%2Fb-log/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idelsink%2Fb-log/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/idelsink","download_url":"https://codeload.github.com/idelsink/b-log/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252922308,"owners_count":21825641,"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":["bash","file","library","logger","logging","stdout","syslog"],"created_at":"2024-07-31T19:01:33.931Z","updated_at":"2025-05-07T17:20:58.959Z","avatar_url":"https://github.com/idelsink.png","language":"Shell","readme":"# bash logger\n\nA library for custom logging in bash called '**[b-log](https://github.com/idelsink/b-log)**'.\nBased on the idea of [log.sh](https://github.com/livibetter-backup/log.sh).\n\n## Features\n\nThis library has the following features:\n\n-   multiple log outputs (stdout, file, syslog)\n-   multiple log levels (FATAL, ERROR, ...)\n-   custom templates\n-   custom error levels\n-   piping of error messages\n\n## Examples\n\nSome small examples to show the basic usage.\nFor a simple complete example, see the [example.sh](./examples/example.sh) file.\n\n### 01 Basic example\n\nSee [this](./examples/01_basic_example.sh) file.\n\n```bash\n#!/usr/bin/env bash\n# example: 01 - basic example\nsource \"$(dirname \"$( realpath ${BASH_SOURCE[0]} )\" )\"/../b-log.sh  # include the script\nLOG_LEVEL_ALL               # set log level to all\nFATAL   \"fatal level\"\nERROR   \"error level\"\nWARN    \"warning level\"\nNOTICE  \"notice level\"\nINFO    \"info level\"\nDEBUG   \"debug level\"\nTRACE   \"trace level\"\n```\n\nTerminal output:  \n![Example 01](./examples/01_basic_example.png \"Example 01 output\")\n\n### 02 Log to file and syslog\n\nSee [this](./examples/02_log_to_file_and_syslog.sh) file.\n\n```bash\n#!/usr/bin/env bash\n# example: 02 - log to file and syslog\nsource \"$(dirname \"$( realpath ${BASH_SOURCE[0]} )\" )\"/../b-log.sh  # include the script\nLOG_LEVEL_ALL               # set log level to all\nB_LOG --file log.txt --file-prefix-enable --file-suffix-enable\nB_LOG --syslog '--tag b-log_example_02'\nFATAL   \"fatal level\"\nERROR   \"error level\"\nWARN    \"warning level\"\nNOTICE  \"notice level\"\nINFO    \"info level\"\nDEBUG   \"debug level\"\nTRACE   \"trace level\"\necho \"Printing the tail of last 7 messages '/var/log/syslog'\"\ntail -n 7 /var/log/syslog\n```\n\nTerminal output:  \n![Example 02](./examples/02_log_to_file_and_syslog.png \"Example 02 output\")\n\n### 03 Custom template\n\nSee [this](./examples/03_custom_level_and_template.sh) file.\n\n```bash\n#!/usr/bin/env bash\n# example: 03 - custom log level and template\nsource \"$(dirname \"$( realpath ${BASH_SOURCE[0]} )\" )\"/../b-log.sh  # include the script\nLOG_LEVEL_ALL               # set log level to all\nFATAL   \"fatal level\"\nERROR   \"error level\"\nWARN    \"warning level\"\nNOTICE  \"notice level\"\nINFO    \"info level\"\nDEBUG   \"debug level\"\nTRACE   \"trace level\"\n# add new log level with the following:\n# level nr: 50\n# level name: EXAMPLE\n# template: timestamp(only time) name [message]\nB_LOG --date-format \"%H:%M:%S.%N\" # set timestamp format\nLOG_LEVELS+=(\"50\" \"EXAMPLE\" \"@12:1@ @2@ [@5@]\" \"\\e[37m\" \"\\e[0m\") # add custom log level\nB_LOG_MESSAGE 50 \"example log message\"\nWARN \"all the normal levels still work, with the new date-format of course\"\n```\n\nTerminal output:  \n![Example 03](./examples/03_custom_level_and_template.png \"Example 03 output\")\n\n## Usage\n\n### Include in script\n\n```bash\n# relative include b-log from file\nsource \"$(dirname \"$( realpath ${BASH_SOURCE[0]} )\" )\"/\u003crelative path to b-log\u003e/b-log.sh\n```\n\n### Parameters\n\nThe `B_LOG` function, acts like the script interface.\nFrom here all the parameters can be set.\n\n```text\nExample of\nb-log v1.2.0\n\nUsage: B_LOG [options]\n  -h, --help              Show usage\n  -V, --version           Version\n  -d, --date-format       Date format used in the log eg. '%Y-%m-%d %H:%M:%S.%N'\n  -o, --stdout            Log over stdout (true/false) default true.\n  -f, --file              File to log to, none set means disabled\n  --file-prefix-enable    Enable the prefix for the log file\n  --file-prefix-disable   Disable the prefix for the log file\n  --file-suffix-enable    Enable the suffix for the log file\n  --file-suffix-disable   Disable the suffix for the log file\n  -s, --syslog            'switches you want to use'. None set means disabled\n                          results in: \"logger 'switches' log-message\"\n  -l, --log-level         The log level\n                          Log levels       : value\n                          ---------------- : -----\n                          LOG_LEVEL_OFF    : 0\n                          LOG_LEVEL_FATAL  : 100\n                          LOG_LEVEL_ERROR  : 200\n                          LOG_LEVEL_WARN   : 300\n                          LOG_LEVEL_NOTICE : 400\n                          LOG_LEVEL_INFO   : 500\n                          LOG_LEVEL_DEBUG  : 600\n                          LOG_LEVEL_TRACE  : 700\n```\n\n#### Log level\n\nSetting the log level can be accomplished in three different ways.  \nFirst via a simple integer value, second via the available parameters\nand last via a function.\n\n```bash\n# Log level OFF\nB_LOG --log-level 0\n# Log level FATAL\nB_LOG --log-level $LOG_LEVEL_FATAL\n# Log level ERROR\nLOG_LEVEL_ERROR # Internal call to B_LOG --log-level $LOG_LEVEL_ERROR\n```\n\n#### Logging\n\nLogging can be done via two ways.  \nFirst there are the predefined functions:\n\n```bash\nFATAL \"fatal level\"\nERROR \"error level\"\nWARN \"warning level\"\nINFO \"info level\"\nDEBUG \"debug level\"\nTRACE \"trace level\"\n```\n\nSecond the underlying generic functions that is called:\n\n```bash\nB_LOG_MESSAGE error_level \"message\"     # error_level is the integer value\nB_LOG_MESSAGE LOG_LEVEL_INFO \"message\"  # send message to INFO\nB_LOG_MESSAGE 400 \"message\"             # 400 is the value of LOG_LEVEL_INFO\n```\n\nThe `B_LOG_MESSAGE` function, which is used to log messages,\ncan also read from stdin.  \nFor example:\n\n```bash\necho \"piped into INFO\" | INFO\necho \"piped into INFO\" | B_LOG_MESSAGE $LOG_LEVEL_INFO\necho \"piped into INFO\" | B_LOG_MESSAGE 400\n```\n\n#### Logging via stdout\n\nStdout is the standard output used. by default this is enabled.\nTo enable or disable this use the following commands:\n\n```bash\nB_LOG --stdout true # enable logging over stdout\nB_LOG --stdout false # disable logging over stdout\n```\n\n#### Logging via a file\n\nBy default, logging to a file is disabled.\nTo enable this, use the following commands:\n\n```bash\nB_LOG --file log/log.txt # logs to a file called log.txt\n# enable the prefix and suffix, these contain the color formats\nB_LOG --file-prefix-enable --file-suffix-enable\n```\n\n#### Logging via the syslog\n\nLogging via the linux `logger` command can be handy in some cases.\nTo enable, use the following:\n\n```bash\n# this will log all the messages to the '/var/log/syslog' with tag 'b-log'\n# for all the parameters available see the man page of logger. 'man logger'\nB_LOG --syslog '--tag b-log'\n```\n\nAs demonstrated the underlying code will simply call the `logger` command with\nthe settings as the parameters to the command.  \nEg. `logger parameters log-message`.\n\n### Templates\n\nThe log templates are base on a string in the `LOG_LEVELS` array.\n\nTemplates are based on numbers between the special character `@`.\nThis can be done in two formats:\n\n-   `@s@`\n-   `@l:s@`\n\n`s` is the selector to select the type in the list below.\n`l` is the length of the string of the selected item.\nIf this is not set, the length that is needed for the string is used.\n\nThe following parameters are supported at this moment:\n\n1.  time-stamp (this uses the date-format that can be customized via the `--date-format` parameter)\n2.  log level name\n3.  function name\n4.  line number\n5.  log message\n6.  space character\n7.  filename\n\nThe default template used, that is stored in `B_LOG_DEFAULT_TEMPLATE`, is:  \n`[@23:1@][@5:2@][@3@:@3:4@] @5@`\n\nThe items used in this template are:\n\n-   timestamp, size 23\n-   log level name, size 5\n-   function name, no limit\n-   line number, size 3\n-   log message, no limit\n\nThe output would be (taken from [example 01](./examples/01_basic_example.sh)):\n\n```text\n[2016-07-02 18:46:02.005][INFO ][main:8  ] info level\n```\n\n### Custom log levels\n\nBesides the default log levels, custom levels can be added.\nAll log levels are stored in an array with the following layout:\n\n-   log level integer value (eg. value 400 for INFO)\n-   log level name (eg. INFO)\n-   log level template (eg. `[@23:1@][@5:2@][@3@:@3:4@] @5@` as default)\n-   log level prefix (something before the log message. eg. color codes)\n-   log level suffix (something after the log message. eg. color codes)\n\nTo add a log level, do the following:\n\n```bash\n# add level with number 50, name EXAMPLE, default template and colors like WARN\nLOG_LEVELS+=(\"50\" \"EXAMPLE\" \"[@23:1@][@5:2@][@3@:@3:4@] @5@\" \"\\e[37m\" \"\\e[0m\")\n# or\nLOG_LEVELS+=(\"50\" \"EXAMPLE\" \"$B_LOG_DEFAULT_TEMPLATE\" \"\\e[37m\" \"\\e[0m\")\n```\n\nSee also [this](./examples/03_custom_level_and_template.sh) example file.\n\n## License\n\n\u003e You can check out the full license [here](./LICENSE)\n\nThis project is licensed under the terms of the **MIT** license.\n","funding_links":[],"categories":["Shell"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidelsink%2Fb-log","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidelsink%2Fb-log","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidelsink%2Fb-log/lists"}