log21 3.1.0__tar.gz → 3.2.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: log21
3
- Version: 3.1.0
3
+ Version: 3.2.0
4
4
  Summary: A simple logging package
5
5
  Keywords: python,log,colorize,color,logging,Python3,CodeWriter21
6
6
  Author: CodeWriter21(Mehrad Pooryoussof)
@@ -94,140 +94,26 @@ pip install git+https://github.com/MPCodeWriter21/log21
94
94
  Changelog
95
95
  ---------
96
96
 
97
- ### v3.1.0
97
+ ### v3.2.0
98
98
 
99
- Change the way `argumentify` handles function parameters to argument-parser arguments
100
- conversion.
99
+ Add `file_mode` and `file_encoding` parameters to `get_logger` for finer control over
100
+ the way a simple logger handles files.
101
101
 
102
- + `POSITIONAL_ONLY` and `VAR_POSITIONAL` parameters will be positional arguments.
103
- + `POSITIONAL_OR_KEYWORD` and `KEYWORD_ONLY` parameters have flags assigned to them.
104
- + `POSITIONAL_OR_KEYWORD` parameters will be required if at least one `KEYWORD_ONLY`
105
- parameter is there, otherwise they are optional.
106
- + `VAR_KEYWORD` parameters are still not supported.
102
+ For even more control you can still define Logger, Handlers, and Formatters manually.
107
103
 
108
- #### Example 1
104
+ #### Example
109
105
 
110
106
  ```python
111
- def main(path: Path, /, output: Path, *, verbose: bool = False):
112
- """Process a file.
107
+ import log21
113
108
 
114
- :param path: The input file path
115
- :param output: The output file
116
- :param verbose: Write more logs to the standard output.
117
- """
118
- ...
119
-
120
-
121
- if __name__ == "__main__":
122
- argumentify(main)
123
- ```
124
-
125
- The help looks like this:
126
-
127
- ```help
128
- usage: test.py [-h] --output OUTPUT [--verbose] path
129
-
130
- Process a file.
131
-
132
- positional arguments:
133
- path The input file path
134
-
135
- options:
136
- -h, --help
137
- show this help message and exit
138
- --output OUTPUT, -o OUTPUT
139
- The output file
140
- --verbose, -v
141
- Write more logs to the standard output.
142
-
143
- ```
144
-
145
- _Note that `path` and `output` are required._
146
-
147
- #### Example 2
148
-
149
- ```python
150
- def main(output: Path, /, *inputs: Path):
151
- """Process multiple files into one.
152
-
153
- :param output: The output file
154
- :param inputs: The path to the input files
155
- """
156
- # Since `inputs` is a VAR_POSITIONAL, while being a positional argument, it can have
157
- # zero length which is in many cases not intended.
158
- # You might want to add a check for its length and raise an ArgumentError if it does
159
- # not match your needs
160
-
161
- # Check if at least one input has been passed and mark the argument as required
162
- # if len(inputs) < 1:
163
- # raise RequiredArgumentError("inputs")
164
-
165
- # Raise an error unless at least two inputs are present
166
- if len(inputs) < 2:
167
- raise ArgumentError(message="You need to pass at least two files as input.")
168
- ...
169
- ```
170
-
171
- The help looks like this:
172
-
173
- ```help
174
- usage: test.py [-h] output [inputs ...]
175
-
176
- Process multiple files into one.
177
-
178
- positional arguments:
179
- output The output file
180
- inputs The path to the input files
181
-
182
- options:
183
- -h, --help
184
- show this help message and exit
109
+ logger = log21.get_logger(
110
+ "My File Logger", show_level=False, show_time=True, file="myapp.log", file_mode="a",
111
+ file_encoding="utf-8"
112
+ )
185
113
 
114
+ logger.info("Hello World!")
186
115
  ```
187
116
 
188
- #### Example 3
189
-
190
- ```python
191
- def main(first_name: str, last_name: str, output: Path, verbose: bool = False):
192
- """Write a greeting message.
193
-
194
- :param first_name: The first name of the user to greet (optional)
195
- :param last_name: The last name of the user to greet (optional)
196
- :param output: The output file (stdout if none is provided)
197
- :param verbose: If provided, will write the debug logs to stdout
198
- """
199
- ...
200
-
201
-
202
- if __name__ == "__main__":
203
- argumentify(main)
204
- ```
205
-
206
- The help looks like this:
207
-
208
- ```help
209
- usage: test.py [-h] [--first-name FIRST_NAME] [--last-name LAST_NAME] [--output OUTPUT]
210
- [--verbose]
211
-
212
- Write a greeting message.
213
-
214
- options:
215
- -h, --help
216
- show this help message and exit
217
- --first-name FIRST_NAME, -f FIRST_NAME
218
- The first name of the user to greet (optional)
219
- --last-name LAST_NAME, -l LAST_NAME
220
- The last name of the user to greet (optional)
221
- --output OUTPUT, -o OUTPUT
222
- The output file (stdout if none is provided)
223
- --verbose, -v
224
- If provided, will write the debug logs to stdout
225
-
226
- ```
227
-
228
- _Note that all the options are optional and default to None. `verbose` is False by
229
- default since a default value is provided for it in function definition._
230
-
231
117
  [Full CHANGELOG](https://github.com/MPCodeWriter21/log21/blob/master/CHANGELOG.md)
232
118
 
233
119
  Usage Examples
@@ -69,140 +69,26 @@ pip install git+https://github.com/MPCodeWriter21/log21
69
69
  Changelog
70
70
  ---------
71
71
 
72
- ### v3.1.0
72
+ ### v3.2.0
73
73
 
74
- Change the way `argumentify` handles function parameters to argument-parser arguments
75
- conversion.
74
+ Add `file_mode` and `file_encoding` parameters to `get_logger` for finer control over
75
+ the way a simple logger handles files.
76
76
 
77
- + `POSITIONAL_ONLY` and `VAR_POSITIONAL` parameters will be positional arguments.
78
- + `POSITIONAL_OR_KEYWORD` and `KEYWORD_ONLY` parameters have flags assigned to them.
79
- + `POSITIONAL_OR_KEYWORD` parameters will be required if at least one `KEYWORD_ONLY`
80
- parameter is there, otherwise they are optional.
81
- + `VAR_KEYWORD` parameters are still not supported.
77
+ For even more control you can still define Logger, Handlers, and Formatters manually.
82
78
 
83
- #### Example 1
79
+ #### Example
84
80
 
85
81
  ```python
86
- def main(path: Path, /, output: Path, *, verbose: bool = False):
87
- """Process a file.
82
+ import log21
88
83
 
89
- :param path: The input file path
90
- :param output: The output file
91
- :param verbose: Write more logs to the standard output.
92
- """
93
- ...
94
-
95
-
96
- if __name__ == "__main__":
97
- argumentify(main)
98
- ```
99
-
100
- The help looks like this:
101
-
102
- ```help
103
- usage: test.py [-h] --output OUTPUT [--verbose] path
104
-
105
- Process a file.
106
-
107
- positional arguments:
108
- path The input file path
109
-
110
- options:
111
- -h, --help
112
- show this help message and exit
113
- --output OUTPUT, -o OUTPUT
114
- The output file
115
- --verbose, -v
116
- Write more logs to the standard output.
117
-
118
- ```
119
-
120
- _Note that `path` and `output` are required._
121
-
122
- #### Example 2
123
-
124
- ```python
125
- def main(output: Path, /, *inputs: Path):
126
- """Process multiple files into one.
127
-
128
- :param output: The output file
129
- :param inputs: The path to the input files
130
- """
131
- # Since `inputs` is a VAR_POSITIONAL, while being a positional argument, it can have
132
- # zero length which is in many cases not intended.
133
- # You might want to add a check for its length and raise an ArgumentError if it does
134
- # not match your needs
135
-
136
- # Check if at least one input has been passed and mark the argument as required
137
- # if len(inputs) < 1:
138
- # raise RequiredArgumentError("inputs")
139
-
140
- # Raise an error unless at least two inputs are present
141
- if len(inputs) < 2:
142
- raise ArgumentError(message="You need to pass at least two files as input.")
143
- ...
144
- ```
145
-
146
- The help looks like this:
147
-
148
- ```help
149
- usage: test.py [-h] output [inputs ...]
150
-
151
- Process multiple files into one.
152
-
153
- positional arguments:
154
- output The output file
155
- inputs The path to the input files
156
-
157
- options:
158
- -h, --help
159
- show this help message and exit
84
+ logger = log21.get_logger(
85
+ "My File Logger", show_level=False, show_time=True, file="myapp.log", file_mode="a",
86
+ file_encoding="utf-8"
87
+ )
160
88
 
89
+ logger.info("Hello World!")
161
90
  ```
162
91
 
163
- #### Example 3
164
-
165
- ```python
166
- def main(first_name: str, last_name: str, output: Path, verbose: bool = False):
167
- """Write a greeting message.
168
-
169
- :param first_name: The first name of the user to greet (optional)
170
- :param last_name: The last name of the user to greet (optional)
171
- :param output: The output file (stdout if none is provided)
172
- :param verbose: If provided, will write the debug logs to stdout
173
- """
174
- ...
175
-
176
-
177
- if __name__ == "__main__":
178
- argumentify(main)
179
- ```
180
-
181
- The help looks like this:
182
-
183
- ```help
184
- usage: test.py [-h] [--first-name FIRST_NAME] [--last-name LAST_NAME] [--output OUTPUT]
185
- [--verbose]
186
-
187
- Write a greeting message.
188
-
189
- options:
190
- -h, --help
191
- show this help message and exit
192
- --first-name FIRST_NAME, -f FIRST_NAME
193
- The first name of the user to greet (optional)
194
- --last-name LAST_NAME, -l LAST_NAME
195
- The last name of the user to greet (optional)
196
- --output OUTPUT, -o OUTPUT
197
- The output file (stdout if none is provided)
198
- --verbose, -v
199
- If provided, will write the debug logs to stdout
200
-
201
- ```
202
-
203
- _Note that all the options are optional and default to None. `verbose` is False by
204
- default since a default value is provided for it in function definition._
205
-
206
92
  [Full CHANGELOG](https://github.com/MPCodeWriter21/log21/blob/master/CHANGELOG.md)
207
93
 
208
94
  Usage Examples
@@ -23,7 +23,7 @@ dependencies = [
23
23
  "webcolors",
24
24
  "docstring-parser"
25
25
  ]
26
- version = "3.1.0"
26
+ version = "3.2.0"
27
27
 
28
28
  [build-system]
29
29
  requires = ["uv_build>=0.8.15,<0.9.0"]
@@ -31,7 +31,7 @@ from .stream_handler import StreamHandler, ColorizingStreamHandler
31
31
  # yapf: enable
32
32
 
33
33
  __author__ = 'CodeWriter21 (Mehrad Pooryoussof)'
34
- __version__ = '3.1.0'
34
+ __version__ = '3.2.0'
35
35
  __github__ = 'https://GitHub.com/MPCodeWriter21/log21'
36
36
  __all__ = [
37
37
  'ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter',
@@ -60,7 +60,8 @@ def _prepare_formatter(
60
60
  colorize_time_and_level: bool = True,
61
61
  level_names: _Optional[_Mapping[int, str]] = None,
62
62
  level_colors: _Optional[_Mapping[int, tuple[str, ...]]] = None,
63
- formatter_class: _Type[_logging.Formatter] = ColorizingFormatter
63
+ formatter_class: _Type[_logging.Formatter] = ColorizingFormatter,
64
+ prefix_carriage_return: bool = True,
64
65
  ) -> _logging.Formatter:
65
66
  # Prepares a formatting if the fmt was None
66
67
  if not fmt:
@@ -70,7 +71,8 @@ def _prepare_formatter(
70
71
  fmt = '[%(levelname)s] ' + fmt
71
72
  if show_time:
72
73
  fmt = '[%(asctime)s] ' + fmt
73
- fmt = '\r' + fmt
74
+ if prefix_carriage_return:
75
+ fmt = '\r' + fmt
74
76
 
75
77
  if level_colors and not issubclass(formatter_class, ColorizingFormatter):
76
78
  warning(
@@ -116,7 +118,10 @@ def get_logger(
116
118
  override: bool = False,
117
119
  level_names: _Optional[_Mapping[int, str]] = None,
118
120
  level_colors: _Optional[_Mapping[int, tuple[str, ...]]] = None,
119
- file: _Optional[_Union[_os.PathLike, str]] = None
121
+ # TODO: Rename file to file_path in one future update
122
+ file: _Optional[_Union[_os.PathLike, str]] = None,
123
+ file_mode: _Optional[str] = None,
124
+ file_encoding: _Optional[str] = None,
120
125
  ) -> Logger:
121
126
  """Returns a logging.Logger with colorizing support.
122
127
 
@@ -182,7 +187,9 @@ def get_logger(
182
187
  :param level_names: Mapping[int, str] = None: You can specify custom level names.
183
188
  :param level_colors: Mapping[int, Tuple[str, ...]] = None: You can specify custom
184
189
  level colors.
185
- :param file: Union[os.PathLike, str] = None: The file to log to
190
+ :param file: Union[os.PathLike, str] = None: The file path to log to
191
+ :param file_mode: str = None: The mode to open file at (Defaults to 'a')
192
+ :param file_encoding: str = None: The file encoding
186
193
  :return: log21.Logger
187
194
  """
188
195
  if not isinstance(name, str):
@@ -209,7 +216,9 @@ def get_logger(
209
216
  _manager.addLogger(name, logger)
210
217
 
211
218
  if file:
212
- file_handler = FileHandler(file)
219
+ file_handler = DecolorizingFileHandler(
220
+ file, mode=file_mode or 'a', encoding=file_encoding
221
+ )
213
222
  file_formatter = _prepare_formatter(
214
223
  fmt,
215
224
  style,
@@ -218,7 +227,8 @@ def get_logger(
218
227
  show_time,
219
228
  False,
220
229
  level_names,
221
- formatter_class=DecolorizingFormatter
230
+ formatter_class=DecolorizingFormatter,
231
+ prefix_carriage_return=False,
222
232
  )
223
233
  file_handler.setFormatter(file_formatter)
224
234
  logger.addHandler(file_handler)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes