tee-logger 6.19__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tee_logger-6.19/PKG-INFO +151 -0
- tee_logger-6.19/README.md +143 -0
- tee_logger-6.19/pyproject.toml +9 -0
- tee_logger-6.19/setup.cfg +4 -0
- tee_logger-6.19/tee_logger.egg-info/PKG-INFO +151 -0
- tee_logger-6.19/tee_logger.egg-info/SOURCES.txt +7 -0
- tee_logger-6.19/tee_logger.egg-info/dependency_links.txt +1 -0
- tee_logger-6.19/tee_logger.egg-info/requires.txt +1 -0
- tee_logger-6.19/tee_logger.egg-info/top_level.txt +1 -0
tee_logger-6.19/PKG-INFO
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: tee-logger
|
|
3
|
+
Version: 6.19
|
|
4
|
+
Summary: A simple wrapper for python logger that also selectively tee logs to stdout
|
|
5
|
+
Requires-Python: >=3.6
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: python-dateutil
|
|
8
|
+
|
|
9
|
+
# Tee_Logger
|
|
10
|
+
|
|
11
|
+
This Python module provides a simple, customizable logger that can direct output to both the console and a log file. It includes utilities for formatting file names, colorizing text output, and performing log maintenance tasks like archiving and deletion of old logs.
|
|
12
|
+
|
|
13
|
+
## Table of Contents
|
|
14
|
+
- [Tee\_Logger](#tee_logger)
|
|
15
|
+
- [Table of Contents](#table-of-contents)
|
|
16
|
+
- [Installation](#installation)
|
|
17
|
+
- [Overview](#overview)
|
|
18
|
+
- [Key Components](#key-components)
|
|
19
|
+
- [bcolors](#bcolors)
|
|
20
|
+
- [abbreviate\_filename](#abbreviate_filename)
|
|
21
|
+
- [printWithColor](#printwithcolor)
|
|
22
|
+
- [teeLogger](#teelogger)
|
|
23
|
+
- [Usage Examples](#usage-examples)
|
|
24
|
+
- [Note: by default, the caller trace back level is 2, which usually will be the line calling for log.](#note-by-default-the-caller-trace-back-level-is-2-which-usually-will-be-the-line-calling-for-log)
|
|
25
|
+
- [Log Maintenance](#log-maintenance)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
Copy `Tee_Logger.py` into your project and ensure you have Python 3 installed. Dependencies:
|
|
32
|
+
- `dateutil` (optional)
|
|
33
|
+
- `base64`, `math`, `functools`, `subprocess`, `tarfile`, `shutil` (standard library modules)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Overview
|
|
38
|
+
|
|
39
|
+
The code offers a logging class called `teeLogger` that can:
|
|
40
|
+
|
|
41
|
+
- Print log messages with or without colors.
|
|
42
|
+
- Write log entries to a file with date-based folder organization.
|
|
43
|
+
- Automatically compress and delete logs based on specified criteria.
|
|
44
|
+
- Provide caller information in log messages (filename, line number).
|
|
45
|
+
|
|
46
|
+
By default, `teeLogger` outputs to the console and a timestamped log file. You can disable file logging or console printing as needed.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Key Components
|
|
51
|
+
|
|
52
|
+
### bcolors
|
|
53
|
+
A helper class providing ANSI escape codes for colored terminal output.
|
|
54
|
+
```python
|
|
55
|
+
class bcolors:
|
|
56
|
+
HEADER = '\033[95m'
|
|
57
|
+
OKBLUE = '\033[94m'
|
|
58
|
+
OKCYAN = '\033[96m'
|
|
59
|
+
OKGREEN = '\033[92m'
|
|
60
|
+
warning = '\033[93m'
|
|
61
|
+
critical = '\033[91m'
|
|
62
|
+
info = '\033[0m'
|
|
63
|
+
debug = '\033[0m'
|
|
64
|
+
ENDC = '\033[0m'
|
|
65
|
+
BOLD = '\033[1m'
|
|
66
|
+
UNDERLINE = '\033[4m'
|
|
67
|
+
```
|
|
68
|
+
### abbreviate_filename
|
|
69
|
+
A function that shortens file names with optional advanced transformations (e.g., using base64 or scientific notation for line numbers). It is used internally to display a concise “filename:lineNumber” in log messages.
|
|
70
|
+
|
|
71
|
+
### printWithColor
|
|
72
|
+
A helper function that prints a plain text string to the console, wrapped in the appropriate ANSI color codes based on a `level`:
|
|
73
|
+
- `info`: bcolors.info
|
|
74
|
+
- `debug`: bcolors.debug
|
|
75
|
+
- `warning`: bcolors.warning
|
|
76
|
+
- `error`: bcolors.warning
|
|
77
|
+
- `critical`: bcolors.critical
|
|
78
|
+
- `ok` or `okgreen`: bcolors.OKGREEN
|
|
79
|
+
- `okblue`: bcolors.OKBLUE
|
|
80
|
+
- `okcyan`: bcolors.OKCYAN
|
|
81
|
+
- default: bcolors.info
|
|
82
|
+
|
|
83
|
+
### teeLogger
|
|
84
|
+
Primary logger class with the following notable parameters:
|
|
85
|
+
|
|
86
|
+
- `systemLogFileDir`: Directory in which log folders are stored. If `/dev/null`, logging to a file is suppressed. Note: if the specifiled dir cannot be accessed, will then try to use `/tmp`
|
|
87
|
+
- `programName`: Name associated with the logs. Defaults to the caller’s filename.
|
|
88
|
+
- `compressLogAfterMonths`: Number of months after which logs are automatically compressed.
|
|
89
|
+
- `deleteLogAfterYears`: Number of years after which logs are deleted.
|
|
90
|
+
- `suppressPrintout`: If set to `True`, console printing is suppressed, but logs still appear in the file.
|
|
91
|
+
- `fileDescriptorLength`: Maximum title span for formatted file name and line number in logs.
|
|
92
|
+
- `noLog`: If `True`, no file logging occurs.
|
|
93
|
+
- `callerStackDepth`: The stack depth for tracing back the caller file path and line number. Default to 2.
|
|
94
|
+
|
|
95
|
+
Methods include:
|
|
96
|
+
- `teeok(msg)`, `ok(msg)`, `info(msg)`, `error(msg)`, `teeerror(msg)`: Log a message at various levels, optionally printing to console.
|
|
97
|
+
- `printTable(data)`: Nicely formats 2D data or a dictionary as a table before logging.
|
|
98
|
+
- `teelog(msg, level)`: General-purpose logger for any level (e.g. `"warning"`, `"critical"`).
|
|
99
|
+
- `cleanup_old_logs()`: Called automatically to compress or remove old logs.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Usage Examples
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from Tee_Logger import teeLogger
|
|
107
|
+
|
|
108
|
+
# Create a logger instance
|
|
109
|
+
tl = teeLogger(systemLogFileDir='.', programName='MyApp', compressLogAfterMonths=2, deleteLogAfterYears=1)
|
|
110
|
+
|
|
111
|
+
# Log messages
|
|
112
|
+
tl.info("This is an info message. ( only log to file )")
|
|
113
|
+
tl.teeerror("This is an error reported to both stdout and file")
|
|
114
|
+
tl.teeok("Operation successful.")
|
|
115
|
+
tl.teeprint("Normal messages.")
|
|
116
|
+
level='critical'
|
|
117
|
+
tl.teelog('Tee Log with level as a variable',level)
|
|
118
|
+
level='warning'
|
|
119
|
+
tl.log('Log with variable level',level)
|
|
120
|
+
```
|
|
121
|
+
Terminal output:
|
|
122
|
+
```
|
|
123
|
+
Log file: <cwd>/MyApp_log/2025-02-10/MyApp_2025-02-10_01-33-26.log
|
|
124
|
+
This is an error reported to both stdout and file
|
|
125
|
+
Operation successful.
|
|
126
|
+
Normal messages.
|
|
127
|
+
Tee Log with level as a variable
|
|
128
|
+
```
|
|
129
|
+

|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
Logger file:
|
|
133
|
+
```log
|
|
134
|
+
2025-02-10 01:33:26,622 [INFO ] [Tee_Logger:357 ] Starting MyApp at 2025-02-10_01-33-26
|
|
135
|
+
2025-02-10 01:33:26,623 [INFO ] [Tee_Logger:357 ] This is an info message. ( only log to file )
|
|
136
|
+
2025-02-10 01:33:26,624 [ERROR ] [Tee_Logger:362 ] This is an error reported to both stdout and file
|
|
137
|
+
2025-02-10 01:33:26,625 [INFO ] [Tee_Logger:340 ] Operation successful.
|
|
138
|
+
2025-02-10 01:33:26,626 [INFO ] [Tee_Logger:354 ] Normal messages.
|
|
139
|
+
2025-02-10 01:33:26,627 [CRITICAL] [Tee_Logger:370 ] Tee Log with level as a variable
|
|
140
|
+
2025-02-10 01:33:26,628 [WARNING ] [Tee_Logger:374 ] Log with variable level
|
|
141
|
+
```
|
|
142
|
+
Note: by default, the caller trace back level is 2, which usually will be the line calling for log.
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Log Maintenance
|
|
146
|
+
|
|
147
|
+
All logs are stored in `programName_log/YYYY-MM-DD` directories. Upon initialization, the logger permanently runs `cleanup_old_logs()` to:
|
|
148
|
+
- Compress logs older than `compressLogAfterMonths`.
|
|
149
|
+
- Delete logs older than `deleteLogAfterYears`.
|
|
150
|
+
|
|
151
|
+
Compression uses `tar` with `xz` if available, otherwise uses Python’s built-in `tarfile` library. The cleanup runs asynchronously using a `ProcessPoolExecutor`.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Tee_Logger
|
|
2
|
+
|
|
3
|
+
This Python module provides a simple, customizable logger that can direct output to both the console and a log file. It includes utilities for formatting file names, colorizing text output, and performing log maintenance tasks like archiving and deletion of old logs.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
- [Tee\_Logger](#tee_logger)
|
|
7
|
+
- [Table of Contents](#table-of-contents)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Overview](#overview)
|
|
10
|
+
- [Key Components](#key-components)
|
|
11
|
+
- [bcolors](#bcolors)
|
|
12
|
+
- [abbreviate\_filename](#abbreviate_filename)
|
|
13
|
+
- [printWithColor](#printwithcolor)
|
|
14
|
+
- [teeLogger](#teelogger)
|
|
15
|
+
- [Usage Examples](#usage-examples)
|
|
16
|
+
- [Note: by default, the caller trace back level is 2, which usually will be the line calling for log.](#note-by-default-the-caller-trace-back-level-is-2-which-usually-will-be-the-line-calling-for-log)
|
|
17
|
+
- [Log Maintenance](#log-maintenance)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Copy `Tee_Logger.py` into your project and ensure you have Python 3 installed. Dependencies:
|
|
24
|
+
- `dateutil` (optional)
|
|
25
|
+
- `base64`, `math`, `functools`, `subprocess`, `tarfile`, `shutil` (standard library modules)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Overview
|
|
30
|
+
|
|
31
|
+
The code offers a logging class called `teeLogger` that can:
|
|
32
|
+
|
|
33
|
+
- Print log messages with or without colors.
|
|
34
|
+
- Write log entries to a file with date-based folder organization.
|
|
35
|
+
- Automatically compress and delete logs based on specified criteria.
|
|
36
|
+
- Provide caller information in log messages (filename, line number).
|
|
37
|
+
|
|
38
|
+
By default, `teeLogger` outputs to the console and a timestamped log file. You can disable file logging or console printing as needed.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Key Components
|
|
43
|
+
|
|
44
|
+
### bcolors
|
|
45
|
+
A helper class providing ANSI escape codes for colored terminal output.
|
|
46
|
+
```python
|
|
47
|
+
class bcolors:
|
|
48
|
+
HEADER = '\033[95m'
|
|
49
|
+
OKBLUE = '\033[94m'
|
|
50
|
+
OKCYAN = '\033[96m'
|
|
51
|
+
OKGREEN = '\033[92m'
|
|
52
|
+
warning = '\033[93m'
|
|
53
|
+
critical = '\033[91m'
|
|
54
|
+
info = '\033[0m'
|
|
55
|
+
debug = '\033[0m'
|
|
56
|
+
ENDC = '\033[0m'
|
|
57
|
+
BOLD = '\033[1m'
|
|
58
|
+
UNDERLINE = '\033[4m'
|
|
59
|
+
```
|
|
60
|
+
### abbreviate_filename
|
|
61
|
+
A function that shortens file names with optional advanced transformations (e.g., using base64 or scientific notation for line numbers). It is used internally to display a concise “filename:lineNumber” in log messages.
|
|
62
|
+
|
|
63
|
+
### printWithColor
|
|
64
|
+
A helper function that prints a plain text string to the console, wrapped in the appropriate ANSI color codes based on a `level`:
|
|
65
|
+
- `info`: bcolors.info
|
|
66
|
+
- `debug`: bcolors.debug
|
|
67
|
+
- `warning`: bcolors.warning
|
|
68
|
+
- `error`: bcolors.warning
|
|
69
|
+
- `critical`: bcolors.critical
|
|
70
|
+
- `ok` or `okgreen`: bcolors.OKGREEN
|
|
71
|
+
- `okblue`: bcolors.OKBLUE
|
|
72
|
+
- `okcyan`: bcolors.OKCYAN
|
|
73
|
+
- default: bcolors.info
|
|
74
|
+
|
|
75
|
+
### teeLogger
|
|
76
|
+
Primary logger class with the following notable parameters:
|
|
77
|
+
|
|
78
|
+
- `systemLogFileDir`: Directory in which log folders are stored. If `/dev/null`, logging to a file is suppressed. Note: if the specifiled dir cannot be accessed, will then try to use `/tmp`
|
|
79
|
+
- `programName`: Name associated with the logs. Defaults to the caller’s filename.
|
|
80
|
+
- `compressLogAfterMonths`: Number of months after which logs are automatically compressed.
|
|
81
|
+
- `deleteLogAfterYears`: Number of years after which logs are deleted.
|
|
82
|
+
- `suppressPrintout`: If set to `True`, console printing is suppressed, but logs still appear in the file.
|
|
83
|
+
- `fileDescriptorLength`: Maximum title span for formatted file name and line number in logs.
|
|
84
|
+
- `noLog`: If `True`, no file logging occurs.
|
|
85
|
+
- `callerStackDepth`: The stack depth for tracing back the caller file path and line number. Default to 2.
|
|
86
|
+
|
|
87
|
+
Methods include:
|
|
88
|
+
- `teeok(msg)`, `ok(msg)`, `info(msg)`, `error(msg)`, `teeerror(msg)`: Log a message at various levels, optionally printing to console.
|
|
89
|
+
- `printTable(data)`: Nicely formats 2D data or a dictionary as a table before logging.
|
|
90
|
+
- `teelog(msg, level)`: General-purpose logger for any level (e.g. `"warning"`, `"critical"`).
|
|
91
|
+
- `cleanup_old_logs()`: Called automatically to compress or remove old logs.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Usage Examples
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from Tee_Logger import teeLogger
|
|
99
|
+
|
|
100
|
+
# Create a logger instance
|
|
101
|
+
tl = teeLogger(systemLogFileDir='.', programName='MyApp', compressLogAfterMonths=2, deleteLogAfterYears=1)
|
|
102
|
+
|
|
103
|
+
# Log messages
|
|
104
|
+
tl.info("This is an info message. ( only log to file )")
|
|
105
|
+
tl.teeerror("This is an error reported to both stdout and file")
|
|
106
|
+
tl.teeok("Operation successful.")
|
|
107
|
+
tl.teeprint("Normal messages.")
|
|
108
|
+
level='critical'
|
|
109
|
+
tl.teelog('Tee Log with level as a variable',level)
|
|
110
|
+
level='warning'
|
|
111
|
+
tl.log('Log with variable level',level)
|
|
112
|
+
```
|
|
113
|
+
Terminal output:
|
|
114
|
+
```
|
|
115
|
+
Log file: <cwd>/MyApp_log/2025-02-10/MyApp_2025-02-10_01-33-26.log
|
|
116
|
+
This is an error reported to both stdout and file
|
|
117
|
+
Operation successful.
|
|
118
|
+
Normal messages.
|
|
119
|
+
Tee Log with level as a variable
|
|
120
|
+
```
|
|
121
|
+

|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
Logger file:
|
|
125
|
+
```log
|
|
126
|
+
2025-02-10 01:33:26,622 [INFO ] [Tee_Logger:357 ] Starting MyApp at 2025-02-10_01-33-26
|
|
127
|
+
2025-02-10 01:33:26,623 [INFO ] [Tee_Logger:357 ] This is an info message. ( only log to file )
|
|
128
|
+
2025-02-10 01:33:26,624 [ERROR ] [Tee_Logger:362 ] This is an error reported to both stdout and file
|
|
129
|
+
2025-02-10 01:33:26,625 [INFO ] [Tee_Logger:340 ] Operation successful.
|
|
130
|
+
2025-02-10 01:33:26,626 [INFO ] [Tee_Logger:354 ] Normal messages.
|
|
131
|
+
2025-02-10 01:33:26,627 [CRITICAL] [Tee_Logger:370 ] Tee Log with level as a variable
|
|
132
|
+
2025-02-10 01:33:26,628 [WARNING ] [Tee_Logger:374 ] Log with variable level
|
|
133
|
+
```
|
|
134
|
+
Note: by default, the caller trace back level is 2, which usually will be the line calling for log.
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Log Maintenance
|
|
138
|
+
|
|
139
|
+
All logs are stored in `programName_log/YYYY-MM-DD` directories. Upon initialization, the logger permanently runs `cleanup_old_logs()` to:
|
|
140
|
+
- Compress logs older than `compressLogAfterMonths`.
|
|
141
|
+
- Delete logs older than `deleteLogAfterYears`.
|
|
142
|
+
|
|
143
|
+
Compression uses `tar` with `xz` if available, otherwise uses Python’s built-in `tarfile` library. The cleanup runs asynchronously using a `ProcessPoolExecutor`.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: tee-logger
|
|
3
|
+
Version: 6.19
|
|
4
|
+
Summary: A simple wrapper for python logger that also selectively tee logs to stdout
|
|
5
|
+
Requires-Python: >=3.6
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: python-dateutil
|
|
8
|
+
|
|
9
|
+
# Tee_Logger
|
|
10
|
+
|
|
11
|
+
This Python module provides a simple, customizable logger that can direct output to both the console and a log file. It includes utilities for formatting file names, colorizing text output, and performing log maintenance tasks like archiving and deletion of old logs.
|
|
12
|
+
|
|
13
|
+
## Table of Contents
|
|
14
|
+
- [Tee\_Logger](#tee_logger)
|
|
15
|
+
- [Table of Contents](#table-of-contents)
|
|
16
|
+
- [Installation](#installation)
|
|
17
|
+
- [Overview](#overview)
|
|
18
|
+
- [Key Components](#key-components)
|
|
19
|
+
- [bcolors](#bcolors)
|
|
20
|
+
- [abbreviate\_filename](#abbreviate_filename)
|
|
21
|
+
- [printWithColor](#printwithcolor)
|
|
22
|
+
- [teeLogger](#teelogger)
|
|
23
|
+
- [Usage Examples](#usage-examples)
|
|
24
|
+
- [Note: by default, the caller trace back level is 2, which usually will be the line calling for log.](#note-by-default-the-caller-trace-back-level-is-2-which-usually-will-be-the-line-calling-for-log)
|
|
25
|
+
- [Log Maintenance](#log-maintenance)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
Copy `Tee_Logger.py` into your project and ensure you have Python 3 installed. Dependencies:
|
|
32
|
+
- `dateutil` (optional)
|
|
33
|
+
- `base64`, `math`, `functools`, `subprocess`, `tarfile`, `shutil` (standard library modules)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Overview
|
|
38
|
+
|
|
39
|
+
The code offers a logging class called `teeLogger` that can:
|
|
40
|
+
|
|
41
|
+
- Print log messages with or without colors.
|
|
42
|
+
- Write log entries to a file with date-based folder organization.
|
|
43
|
+
- Automatically compress and delete logs based on specified criteria.
|
|
44
|
+
- Provide caller information in log messages (filename, line number).
|
|
45
|
+
|
|
46
|
+
By default, `teeLogger` outputs to the console and a timestamped log file. You can disable file logging or console printing as needed.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Key Components
|
|
51
|
+
|
|
52
|
+
### bcolors
|
|
53
|
+
A helper class providing ANSI escape codes for colored terminal output.
|
|
54
|
+
```python
|
|
55
|
+
class bcolors:
|
|
56
|
+
HEADER = '\033[95m'
|
|
57
|
+
OKBLUE = '\033[94m'
|
|
58
|
+
OKCYAN = '\033[96m'
|
|
59
|
+
OKGREEN = '\033[92m'
|
|
60
|
+
warning = '\033[93m'
|
|
61
|
+
critical = '\033[91m'
|
|
62
|
+
info = '\033[0m'
|
|
63
|
+
debug = '\033[0m'
|
|
64
|
+
ENDC = '\033[0m'
|
|
65
|
+
BOLD = '\033[1m'
|
|
66
|
+
UNDERLINE = '\033[4m'
|
|
67
|
+
```
|
|
68
|
+
### abbreviate_filename
|
|
69
|
+
A function that shortens file names with optional advanced transformations (e.g., using base64 or scientific notation for line numbers). It is used internally to display a concise “filename:lineNumber” in log messages.
|
|
70
|
+
|
|
71
|
+
### printWithColor
|
|
72
|
+
A helper function that prints a plain text string to the console, wrapped in the appropriate ANSI color codes based on a `level`:
|
|
73
|
+
- `info`: bcolors.info
|
|
74
|
+
- `debug`: bcolors.debug
|
|
75
|
+
- `warning`: bcolors.warning
|
|
76
|
+
- `error`: bcolors.warning
|
|
77
|
+
- `critical`: bcolors.critical
|
|
78
|
+
- `ok` or `okgreen`: bcolors.OKGREEN
|
|
79
|
+
- `okblue`: bcolors.OKBLUE
|
|
80
|
+
- `okcyan`: bcolors.OKCYAN
|
|
81
|
+
- default: bcolors.info
|
|
82
|
+
|
|
83
|
+
### teeLogger
|
|
84
|
+
Primary logger class with the following notable parameters:
|
|
85
|
+
|
|
86
|
+
- `systemLogFileDir`: Directory in which log folders are stored. If `/dev/null`, logging to a file is suppressed. Note: if the specifiled dir cannot be accessed, will then try to use `/tmp`
|
|
87
|
+
- `programName`: Name associated with the logs. Defaults to the caller’s filename.
|
|
88
|
+
- `compressLogAfterMonths`: Number of months after which logs are automatically compressed.
|
|
89
|
+
- `deleteLogAfterYears`: Number of years after which logs are deleted.
|
|
90
|
+
- `suppressPrintout`: If set to `True`, console printing is suppressed, but logs still appear in the file.
|
|
91
|
+
- `fileDescriptorLength`: Maximum title span for formatted file name and line number in logs.
|
|
92
|
+
- `noLog`: If `True`, no file logging occurs.
|
|
93
|
+
- `callerStackDepth`: The stack depth for tracing back the caller file path and line number. Default to 2.
|
|
94
|
+
|
|
95
|
+
Methods include:
|
|
96
|
+
- `teeok(msg)`, `ok(msg)`, `info(msg)`, `error(msg)`, `teeerror(msg)`: Log a message at various levels, optionally printing to console.
|
|
97
|
+
- `printTable(data)`: Nicely formats 2D data or a dictionary as a table before logging.
|
|
98
|
+
- `teelog(msg, level)`: General-purpose logger for any level (e.g. `"warning"`, `"critical"`).
|
|
99
|
+
- `cleanup_old_logs()`: Called automatically to compress or remove old logs.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Usage Examples
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from Tee_Logger import teeLogger
|
|
107
|
+
|
|
108
|
+
# Create a logger instance
|
|
109
|
+
tl = teeLogger(systemLogFileDir='.', programName='MyApp', compressLogAfterMonths=2, deleteLogAfterYears=1)
|
|
110
|
+
|
|
111
|
+
# Log messages
|
|
112
|
+
tl.info("This is an info message. ( only log to file )")
|
|
113
|
+
tl.teeerror("This is an error reported to both stdout and file")
|
|
114
|
+
tl.teeok("Operation successful.")
|
|
115
|
+
tl.teeprint("Normal messages.")
|
|
116
|
+
level='critical'
|
|
117
|
+
tl.teelog('Tee Log with level as a variable',level)
|
|
118
|
+
level='warning'
|
|
119
|
+
tl.log('Log with variable level',level)
|
|
120
|
+
```
|
|
121
|
+
Terminal output:
|
|
122
|
+
```
|
|
123
|
+
Log file: <cwd>/MyApp_log/2025-02-10/MyApp_2025-02-10_01-33-26.log
|
|
124
|
+
This is an error reported to both stdout and file
|
|
125
|
+
Operation successful.
|
|
126
|
+
Normal messages.
|
|
127
|
+
Tee Log with level as a variable
|
|
128
|
+
```
|
|
129
|
+

|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
Logger file:
|
|
133
|
+
```log
|
|
134
|
+
2025-02-10 01:33:26,622 [INFO ] [Tee_Logger:357 ] Starting MyApp at 2025-02-10_01-33-26
|
|
135
|
+
2025-02-10 01:33:26,623 [INFO ] [Tee_Logger:357 ] This is an info message. ( only log to file )
|
|
136
|
+
2025-02-10 01:33:26,624 [ERROR ] [Tee_Logger:362 ] This is an error reported to both stdout and file
|
|
137
|
+
2025-02-10 01:33:26,625 [INFO ] [Tee_Logger:340 ] Operation successful.
|
|
138
|
+
2025-02-10 01:33:26,626 [INFO ] [Tee_Logger:354 ] Normal messages.
|
|
139
|
+
2025-02-10 01:33:26,627 [CRITICAL] [Tee_Logger:370 ] Tee Log with level as a variable
|
|
140
|
+
2025-02-10 01:33:26,628 [WARNING ] [Tee_Logger:374 ] Log with variable level
|
|
141
|
+
```
|
|
142
|
+
Note: by default, the caller trace back level is 2, which usually will be the line calling for log.
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Log Maintenance
|
|
146
|
+
|
|
147
|
+
All logs are stored in `programName_log/YYYY-MM-DD` directories. Upon initialization, the logger permanently runs `cleanup_old_logs()` to:
|
|
148
|
+
- Compress logs older than `compressLogAfterMonths`.
|
|
149
|
+
- Delete logs older than `deleteLogAfterYears`.
|
|
150
|
+
|
|
151
|
+
Compression uses `tar` with `xz` if available, otherwise uses Python’s built-in `tarfile` library. The cleanup runs asynchronously using a `ProcessPoolExecutor`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
python-dateutil
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
screenshots
|