<commit_before># bash logger

A library for custom logging in bash called '**[b-log](https://github.com/idelsink/b-log)**'.
Based on the idea of [log.sh](https://github.com/livibetter-backup/log.sh).

## Features

This library has the following features:

-   multiple log outputs (stdout, file, syslog)
-   multiple log levels (FATAL, ERROR, ...)
-   custom templates
-   custom error levels
-   piping of error messages

## Examples

Some small examples to show the basic usage.
For a simple complete example, see the [example.sh](./examples/example.sh) file.

### 01 Basic example

See [this](./examples/01_basic_example.sh) file.

```sh
source ${0%/*}/../b-log.sh  # include the script
LOG_LEVEL_ALL               # set log level to all
FATAL   "fatal level"
ERROR   "error level"
WARN    "warning level"
INFO    "info level"
DEBUG   "debug level"
TRACE   "trace level"
```

Terminal output:  
![Example 01](/examples/01_basic_example.png "Example 01 output")

### 02 Log to file and syslog

See [this](./examples/02_log_to_file_and_syslog.sh) file.

```sh
source ${0%/*}/../b-log.sh  # include the script
LOG_LEVEL_ALL               # set log level to all
B_LOG --file log.txt --file-prefix-enable --file-suffix-enable
B_LOG --syslog '--tag b-log_example_02'
FATAL   "fatal level"
ERROR   "error level"
WARN    "warning level"
INFO    "info level"
DEBUG   "debug level"
TRACE   "trace level"
echo "Printing the tail of '/var/log/syslog'"
tail -n 5 /var/log/syslog
```

Terminal output:  
![Example 02](/examples/02_log_to_file_and_syslog.png "Example 02 output")

### 03 Custom template

See [this](./examples/03_custom_log_level_and_template.sh) file.

```sh
#!/bin/bash
source ${0%/*}/../b-log.sh  # include the script
LOG_LEVEL_ALL               # set log level to all
FATAL   "fatal level"
ERROR   "error level"
WARN    "warning level"
INFO    "info level"
DEBUG   "debug level"
TRACE   "trace level"
# add new log level with the following:
# level nr: 50
# level name: EXAMPLE
# template: timestamp(only time) name [message]
B_LOG --date-format "%H:%M:%S.%N" # set timestamp format
LOG_LEVELS+=("50" "EXAMPLE" "@12:1@ @2@ [@5@]" "\e[37m" "\e[0m") # add custom log level
B_LOG_MESSAGE 50 "example log message"
WARN "all the normal levels still work, with the new date-format of course"
```

Terminal output:  
![Example 03](/examples/03_custom_level_and_template.png "Example 03 output")

## Usage

### Include in script

```sh
# relative include b-log
source ${0%/*}/../b-log.sh
```

### Parameters

The `B_LOG` function, acts like the script interface.
From here all the parameters can be set.

```text
Usage: B_LOG [options]
  -h --help               Show usage
  -V --version            Version
  -d --date-format        Date format used in the log eg. '%Y-%m-%d %H:%M:%S.%N'
  -o --stdout             Log over stdout (true/false) default true.
  -f --file               File to log to, none set means disabled
  --file-prefix-enable    Enable the prefix for the log file
  --file-prefix-disable   Disable the prefix for the log file
  --file-suffix-enable    Enable the suffix for the log file
  --file-suffix-disable   Disable the suffix for the log file
  -s --syslog             'switches you want to use'. None set means disabled
                          results in: "logger 'switches' log-message"
  -l --log-level          The log level
                          Log levels       : value<commit_msg>fix relative links to images<commit_after># bash logger

A library for custom logging in bash called '**[b-log](https://github.com/idelsink/b-log)**'.
Based on the idea of [log.sh](https://github.com/livibetter-backup/log.sh).

## Features

This library has the following features:

-   multiple log outputs (stdout, file, syslog)
-   multiple log levels (FATAL, ERROR, ...)
-   custom templates
-   custom error levels
-   piping of error messages

## Examples

Some small examples to show the basic usage.
For a simple complete example, see the [example.sh](./examples/example.sh) file.

### 01 Basic example

See [this](./examples/01_basic_example.sh) file.

```sh
source ${0%/*}/../b-log.sh  # include the script
LOG_LEVEL_ALL               # set log level to all
FATAL   "fatal level"
ERROR   "error level"
WARN    "warning level"
INFO    "info level"
DEBUG   "debug level"
TRACE   "trace level"
```

Terminal output:  
![Example 01](./examples/01_basic_example.png "Example 01 output")

### 02 Log to file and syslog

See [this](./examples/02_log_to_file_and_syslog.sh) file.

```sh
source ${0%/*}/../b-log.sh  # include the script
LOG_LEVEL_ALL               # set log level to all
B_LOG --file log.txt --file-prefix-enable --file-suffix-enable
B_LOG --syslog '--tag b-log_example_02'
FATAL   "fatal level"
ERROR   "error level"
WARN    "warning level"
INFO    "info level"
DEBUG   "debug level"
TRACE   "trace level"
echo "Printing the tail of '/var/log/syslog'"
tail -n 5 /var/log/syslog
```

Terminal output:  
![Example 02](./examples/02_log_to_file_and_syslog.png "Example 02 output")

### 03 Custom template

See [this](./examples/03_custom_log_level_and_template.sh) file.

```sh
#!/bin/bash
source ${0%/*}/../b-log.sh  # include the script
LOG_LEVEL_ALL               # set log level to all
FATAL   "fatal level"
ERROR   "error level"
WARN    "warning level"
INFO    "info level"
DEBUG   "debug level"
TRACE   "trace level"
# add new log level with the following:
# level nr: 50
# level name: EXAMPLE
# template: timestamp(only time) name [message]
B_LOG --date-format "%H:%M:%S.%N" # set timestamp format
LOG_LEVELS+=("50" "EXAMPLE" "@12:1@ @2@ [@5@]" "\e[37m" "\e[0m") # add custom log level
B_LOG_MESSAGE 50 "example log message"
WARN "all the normal levels still work, with the new date-format of course"
```

Terminal output:  
![Example 03](./examples/03_custom_level_and_template.png "Example 03 output")

## Usage

### Include in script

```sh
# relative include b-log
source ${0%/*}/../b-log.sh
```

### Parameters

The `B_LOG` function, acts like the script interface.
From here all the parameters can be set.

```text
Usage: B_LOG [options]
  -h --help               Show usage
  -V --version            Version
  -d --date-format        Date format used in the log eg. '%Y-%m-%d %H:%M:%S.%N'
  -o --stdout             Log over stdout (true/false) default true.
  -f --file               File to log to, none set means disabled
  --file-prefix-enable    Enable the prefix for the log file
  --file-prefix-disable   Disable the prefix for the log file
  --file-suffix-enable    Enable the suffix for the log file
  --file-suffix-disable   Disable the suffix for the log file
  -s --syslog             'switches you want to use'. None set means disabled
                          results in: "logger 'switches' log-message"
  -l --log-level          The log level
                          Log levels       : value
{'fraction_non_alphanumeric': 0.1272965879265092, 'fraction_numerical': 0.014873140857392825, 'mean_word_length': 2.8620495495495497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7090820', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2168, 'n_words': 785}