Skip to content

Commit 0ea25af

Browse files
authored
Merge pull request #35 from bybatkhuu/dev
Major update: enhance and refactor logging module with new features.
2 parents ea587cb + 11a7b7f commit 0ea25af

31 files changed

+1524
-1357
lines changed

.env.example

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# ENV=LOCAL
22
# DEBUG=false
33
# TZ=UTC
4-
5-
# BEANS_LOGGING_DISABLE_DEFAULT=false
6-
# BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
7-
# BEANS_LOGGING_LOGS_DIR="./logs"

README.md

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,18 @@ logger.info("Logging info.")
155155

156156
```yml
157157
logger:
158-
app_name: "my-app"
159-
level: "TRACE"
160-
file:
161-
log_handlers:
158+
app_name: my-app
159+
default:
160+
level:
161+
base: TRACE
162+
handlers:
163+
default.all.file_handler:
162164
enabled: true
163-
json_handlers:
165+
default.err.file_handler:
166+
enabled: true
167+
default.all.json_handler:
168+
enabled: true
169+
default.err.json_handler:
164170
enabled: true
165171
```
166172
@@ -211,28 +217,28 @@ python ./main.py
211217
**Output**:
212218

213219
```txt
214-
[2023-09-01 00:00:00.000 +09:00 | TRACE | beans_logging._base:478]: Intercepted modules: ['concurrent', 'concurrent.futures', 'asyncio']; Muted modules: [];
215-
[2023-09-01 00:00:00.000 +09:00 | TRACE | __main__:7]: Tracing...
216-
[2023-09-01 00:00:00.000 +09:00 | DEBUG | __main__:8]: Debugging...
217-
[2023-09-01 00:00:00.000 +09:00 | INFO | __main__:9]: Logging info.
218-
[2023-09-01 00:00:00.000 +09:00 | OK | __main__:10]: Success.
219-
[2023-09-01 00:00:00.000 +09:00 | WARN | __main__:11]: Warning something.
220-
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:12]: Error occured.
221-
[2023-09-01 00:00:00.000 +09:00 | CRIT | __main__:13]: CRITICAL ERROR.
222-
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:25]: division by zero
223-
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:32]: Show me, what value is wrong:
220+
[2025-11-01 00:00:00.735 +09:00 | TRACE | beans_logging._intercept:96]: Intercepted modules: ['potato_util._base', 'potato_util.io', 'concurrent', 'concurrent.futures', 'asyncio', 'potato_util.io._sync', 'potato_util']; Muted modules: [];
221+
[2025-11-01 00:00:00.736 +09:00 | TRACE | __main__:6]: Tracing...
222+
[2025-11-01 00:00:00.736 +09:00 | DEBUG | __main__:7]: Debugging...
223+
[2025-11-01 00:00:00.736 +09:00 | INFO | __main__:8]: Logging info.
224+
[2025-11-01 00:00:00.736 +09:00 | OK | __main__:9]: Success.
225+
[2025-11-01 00:00:00.736 +09:00 | WARN | __main__:10]: Warning something.
226+
[2025-11-01 00:00:00.736 +09:00 | ERROR | __main__:11]: Error occured.
227+
[2025-11-01 00:00:00.736 +09:00 | CRIT | __main__:12]: CRITICAL ERROR.
228+
[2025-11-01 00:00:00.736 +09:00 | ERROR | __main__:24]: division by zero
229+
[2025-11-01 00:00:00.737 +09:00 | ERROR | __main__:31]: Show me, what value is wrong:
224230
Traceback (most recent call last):
225231
226-
> File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 30, in <module>
232+
> File "/home/user/workspaces/projects/my/module-python-logging/examples/simple/./main.py", line 29, in <module>
227233
nested(0)
228-
└ <function nested at 0x10802a4c0>
234+
└ <function nested at 0x102f37910>
229235
230-
File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 23, in nested
236+
File "/home/user/workspaces/projects/my/module-python-logging/examples/simple/./main.py", line 22, in nested
231237
divide(5, c)
232238
│ └ 0
233-
└ <function divide at 0x1052f31f0>
239+
└ <function divide at 0x102f377f0>
234240
235-
File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 17, in divide
241+
File "/home/user/workspaces/projects/my/module-python-logging/examples/simple/./main.py", line 16, in divide
236242
_result = a / b
237243
│ └ 0
238244
└ 5
@@ -250,37 +256,54 @@ ZeroDivisionError: division by zero
250256

251257
```yaml
252258
logger:
253-
# app_name: "app"
254-
level: "INFO"
255-
use_diagnose: false
256-
stream:
257-
use_color: true
258-
use_icon: false
259-
format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{level_short:<5}</level> | <w>{name}:{line}</w>]: <level>{message}</level>"
260-
std_handler:
261-
enabled: true
262-
file:
263-
logs_dir: "./logs"
264-
rotate_size: 10000000 # 10MB
265-
rotate_time: "00:00:00"
266-
backup_count: 90
267-
log_handlers:
268-
enabled: false
269-
format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}"
270-
log_path: "{app_name}.std.all.log"
271-
err_path: "{app_name}.std.err.log"
272-
json_handlers:
273-
enabled: false
274-
use_custom: false
275-
log_path: "{app_name}.json.all.log"
276-
err_path: "{app_name}.json.err.log"
259+
# app_name: app
260+
default:
261+
level:
262+
base: INFO
263+
err: WARNING
264+
std:
265+
format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{extra[level_short]:<5}</level> | <w>{name}:{line}</w>]: <level>{message}</level>"
266+
colorize: true
267+
format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {extra[level_short]:<5} | {name}:{line}]: {message}"
268+
file:
269+
logs_dir: "./logs"
270+
rotate_size: 10000000
271+
rotate_time: "00:00:00"
272+
retention: 90
273+
encoding: utf8
274+
plain:
275+
log_path: "{app_name}.all.log"
276+
err_path: "{app_name}.err.log"
277+
json:
278+
log_path: "json/{app_name}.json.all.log"
279+
err_path: "json/{app_name}.json.err.log"
280+
custom_serialize: false
277281
intercept:
278-
auto_load:
279-
enabled: true
280-
only_base: false
281-
ignore_modules: []
282+
enabled: true
283+
only_base: false
284+
ignore_modules: []
282285
include_modules: []
283286
mute_modules: []
287+
handlers:
288+
default.all.std_handler:
289+
type: STD
290+
enabled: true
291+
default.all.file_handler:
292+
type: FILE
293+
enabled: false
294+
default.err.file_handler:
295+
type: FILE
296+
error: true
297+
enabled: false
298+
default.all.json_handler:
299+
type: FILE
300+
serialize: true
301+
enabled: false
302+
default.err.json_handler:
303+
type: FILE
304+
serialize: true
305+
error: true
306+
enabled: false
284307
extra:
285308
```
286309

@@ -292,10 +315,6 @@ logger:
292315
# ENV=LOCAL
293316
# DEBUG=false
294317
# TZ=UTC
295-
296-
# BEANS_LOGGING_DISABLE_DEFAULT=false
297-
# BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
298-
# BEANS_LOGGING_LOGS_DIR="./logs"
299318
```
300319

301320
---

examples/simple/configs/logger.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
logger:
2-
app_name: "my-app"
3-
level: "TRACE"
4-
file:
5-
log_handlers:
2+
app_name: my-app
3+
default:
4+
level:
5+
base: TRACE
6+
handlers:
7+
default.all.file_handler:
68
enabled: true
7-
json_handlers:
9+
default.err.file_handler:
10+
enabled: true
11+
default.all.json_handler:
12+
enabled: true
13+
default.err.json_handler:
814
enabled: true

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
PyYAML>=6.0.2,<7.0
2-
pydantic[timezone]>=2.0.3,!=2.1.0,<3.0.0
1+
pydantic[timezone]>=2.5.3,<3.0.0
32
loguru>=0.7.3,<1.0.0
3+
potato_util>=0.2.0,<1.0.0

scripts/build.sh

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44

55
## --- Base --- ##
6-
# Getting path of this script file:
7-
_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
6+
_SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-"$0"}")" >/dev/null 2>&1 && pwd -P)"
87
_PROJECT_DIR="$(cd "${_SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd)"
98
cd "${_PROJECT_DIR}" || exit 2
109

1110

12-
if [ -z "$(which python)" ]; then
13-
echo "[ERROR]: 'python' not found or not installed!"
11+
if ! command -v python >/dev/null 2>&1; then
12+
echo "[ERROR]: Not found 'python' command, please install it first!" >&2
1413
exit 1
1514
fi
1615

@@ -30,53 +29,80 @@ _IS_STAGING=true
3029
## --- Variables --- ##
3130

3231

33-
## --- Main --- ##
34-
main()
35-
{
36-
## --- Menu arguments --- ##
37-
if [ -n "${1:-}" ]; then
38-
local _input
39-
for _input in "${@:-}"; do
40-
case ${_input} in
41-
-c | --disable-clean)
42-
_IS_CLEAN=false
43-
shift;;
44-
-t | --test)
45-
_IS_TEST=true
46-
shift;;
47-
-u | --upload)
48-
_IS_UPLOAD=true
49-
shift;;
50-
-p | --production)
51-
_IS_STAGING=false
52-
shift;;
53-
*)
54-
echo "[ERROR]: Failed to parsing input -> ${_input}!"
55-
echo "[INFO]: USAGE: ${0} -c, --disable-clean | -t, --test | -u, --upload | -p, --production"
56-
exit 1;;
57-
esac
58-
done
59-
fi
60-
## --- Menu arguments --- ##
32+
## --- Menu arguments --- ##
33+
_usage_help() {
34+
cat <<EOF
35+
USAGE: ${0} [options]
6136
37+
OPTIONS:
38+
-c, --disable-clean Disable clean process. Default: true
39+
-t, --test Enable test mode. Default: false
40+
-u, --upload Enable upload mode. Default: false
41+
-p, --production Disable staging mode. Default: true
42+
-h, --help Show this help message.
6243
63-
if [ "${_IS_UPLOAD}" == true ]; then
64-
if [ -z "$(which twine)" ]; then
65-
echo "[ERROR]: 'twine' not found or not installed!"
66-
exit 1
67-
fi
44+
EXAMPLES:
45+
${0} --test
46+
${0} -c -u -p
47+
EOF
48+
}
49+
50+
while [ $# -gt 0 ]; do
51+
case "${1}" in
52+
-c | --disable-clean)
53+
_IS_CLEAN=false
54+
shift;;
55+
-t | --test)
56+
_IS_TEST=true
57+
shift;;
58+
-u | --upload)
59+
_IS_UPLOAD=true
60+
shift;;
61+
-p | --production)
62+
_IS_STAGING=false
63+
shift;;
64+
-h | --help)
65+
_usage_help
66+
exit 0;;
67+
*)
68+
echo "[ERROR]: Failed to parse argument -> ${1}!" >&2
69+
_usage_help
70+
exit 1;;
71+
esac
72+
done
73+
## --- Menu arguments --- ##
74+
75+
76+
if [ "${_IS_UPLOAD}" == true ]; then
77+
if ! command -v twine >/dev/null 2>&1; then
78+
echo "[ERROR]: Not found 'twine' command, please install it first!" >&2
79+
exit 1
6880
fi
81+
fi
6982

7083

71-
if [ "${_IS_CLEAN}" == true ]; then
72-
./scripts/clean.sh || exit 2
84+
if [ "${_IS_CLEAN}" == true ]; then
85+
if [ ! -f ./scripts/clean.sh ]; then
86+
echo "[ERROR]: 'clean.sh' script not found!" >&2
87+
exit 1
7388
fi
7489

75-
if [ "${_IS_TEST}" == true ]; then
76-
./scripts/test.sh || exit 2
90+
./scripts/clean.sh || exit 2
91+
fi
92+
93+
if [ "${_IS_TEST}" == true ]; then
94+
if [ ! -f ./scripts/test.sh ]; then
95+
echo "[ERROR]: 'test.sh' script not found!" >&2
96+
exit 1
7797
fi
7898

99+
./scripts/test.sh || exit 2
100+
fi
101+
79102

103+
## --- Main --- ##
104+
main()
105+
{
80106
echo "[INFO]: Building package..."
81107
# python setup.py sdist bdist_wheel || exit 2
82108
python -m build || exit 2
@@ -98,5 +124,5 @@ main()
98124
fi
99125
}
100126

101-
main "${@:-}"
127+
main
102128
## --- Main --- ##

0 commit comments

Comments
 (0)