MapleX 3.1.0.dev5__tar.gz → 3.1.2__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.
- {maplex-3.1.0.dev5/src/MapleX.egg-info → maplex-3.1.2}/PKG-INFO +1 -1
- {maplex-3.1.0.dev5 → maplex-3.1.2}/pyproject.toml +1 -1
- maplex-3.1.2/readmes/LoggingBestPractice.md +116 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/readmes/README_Json.md +20 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2/src/MapleX.egg-info}/PKG-INFO +1 -1
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/__init__.py +1 -1
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/library/logger/file_handler.py +1 -1
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/library/logger/formatter.py +1 -1
- maplex-3.1.0.dev5/readmes/LoggingBestPractice.md +0 -20
- {maplex-3.1.0.dev5 → maplex-3.1.2}/LICENSE +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/MANIFEST.in +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/README.md +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/logErrorOutputSample.png +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/logOutputSample.png +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/readmes/README_ConsoleColors.md +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/readmes/README_Exceptions.md +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/readmes/README_Logger.md +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/readmes/README_MapleTree.md +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/setup.cfg +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/MapleX.egg-info/SOURCES.txt +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/MapleX.egg-info/dependency_links.txt +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/MapleX.egg-info/requires.txt +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/MapleX.egg-info/top_level.txt +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/jsonHandler.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/library/logger/__init__.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/library/logger/config.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/library/logger/consts.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/library/logger/log_levels.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/library/logger/utilities.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/mapleColors.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/mapleExceptions.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/mapleLogger.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/mapleTreeEditor.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/src/maplex/utils.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/tests/test_logger_unittest.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/tests/test_maplejson_unittest.py +0 -0
- {maplex-3.1.0.dev5 → maplex-3.1.2}/tests/test_mapletree_unittest.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: MapleX
|
|
3
|
-
Version: 3.1.
|
|
3
|
+
Version: 3.1.2
|
|
4
4
|
Summary: A Python library for simple logging, json file operations, Maple file format operations, and console color utilities.
|
|
5
5
|
Author: Ryuji Hazama
|
|
6
6
|
Project-URL: PyPI, https://pypi.org/project/MapleX/
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "MapleX"
|
|
7
|
-
version = "3.1.
|
|
7
|
+
version = "3.1.2"
|
|
8
8
|
description = """A Python library for simple logging, json file operations, Maple file format operations, and console color utilities."""
|
|
9
9
|
keywords = ["logging", "logger", "json", "file operations", "maple file format", "console colors"]
|
|
10
10
|
readme = "README.md"
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Logging Best Practice
|
|
2
|
+
|
|
3
|
+
## What Is "Logging"
|
|
4
|
+
|
|
5
|
+
### The flight recorder of the application
|
|
6
|
+
|
|
7
|
+
When you are developing an application, you might experience that your application is crashing silently. Then you will insert a bunch of `print()` lines to determine where the application failed, and what was causing the error. That is a log, and those outputs are showing the exact path of your process, and help you understand what is working correctly, why, and where your code failed after the application halts.
|
|
8
|
+
|
|
9
|
+
However, those logs are disappearing when you close the output terminal, or the terminal was automatically closed by the application. That is why you need to output logs to a file like a flight recorder in a black box.
|
|
10
|
+
|
|
11
|
+
### Four W's of Logging
|
|
12
|
+
|
|
13
|
+
Every time the event occurs, the logger should capture the "Four W's" to get the details of the event.
|
|
14
|
+
|
|
15
|
+
- **When** — When the event occurred.
|
|
16
|
+
- **Where** — Where the event happened.
|
|
17
|
+
- **What** — What was the event.
|
|
18
|
+
- **Weight** — How serious is the event?
|
|
19
|
+
|
|
20
|
+
### Logging vs `print()`
|
|
21
|
+
|
|
22
|
+
Many beginner developers use `print()` to see what their code is doing. While `print()` works for quick debugging, it is a 'disposable' way to log, and it is not recommended for production code. Here are some reasons why:
|
|
23
|
+
|
|
24
|
+
#### Lack of control
|
|
25
|
+
|
|
26
|
+
With `print()`, you have no filtering options. You see everything or nothing. With a logger, you can filter logs by severity level. You can choose what to see depending on the situation. For example, you can set the logger to only show warnings and errors in production, while showing debug information during development.
|
|
27
|
+
|
|
28
|
+
Also, with `print()`, you need to change your code to toggle the logging on and off. With a logger, you can easily enable or disable logging by changing the configuration and no code changes are required.
|
|
29
|
+
|
|
30
|
+
#### Lifetime
|
|
31
|
+
|
|
32
|
+
Logs created with `print()` disappear once the program ends or the terminal is closed. In contrast, logs created with a logger can be saved to a file, allowing you to review them later. This is especially useful for debugging issues that occur in production environments.
|
|
33
|
+
|
|
34
|
+
#### Context
|
|
35
|
+
|
|
36
|
+
Loggers can automatically include contextual information such as timestamps, file names, line numbers, and function names. This information can be invaluable when trying to understand the flow of your application and identify where issues are occurring. With `print()`, you would need to manually include this information in every log statement, which can be error-prone and time-consuming.
|
|
37
|
+
|
|
38
|
+
#### Why you log
|
|
39
|
+
|
|
40
|
+
Logging is essential for understanding the behavior of your application, especially when things go wrong. You don't log for the things go right, but for the things that go wrong and you aren't there to see it happen.
|
|
41
|
+
|
|
42
|
+
Logging is like a flight recorder. You can see what happened before the crash, and you can understand why the crash happened. If the log disappears because of the crash, then you have no way to understand what happened, and it will be very difficult to find the cause of the crash. But if the log is saved to a file like a flight recorder in a black box, then you can review the log and fix the issue more quickly.
|
|
43
|
+
|
|
44
|
+
## Outputs
|
|
45
|
+
|
|
46
|
+
### What to log
|
|
47
|
+
|
|
48
|
+
You should log the events that are important for understanding the behavior of your application, especially when things go wrong. This includes:
|
|
49
|
+
|
|
50
|
+
#### Errors and exceptions
|
|
51
|
+
|
|
52
|
+
Errors and exceptions are the most important events to log, as they indicate that something went wrong in your application. Also, many logger libraries can automatically capture the stack trace when an exception occurs, which can be very helpful for debugging. Those logs can help you understand what went wrong, where it went wrong, and why it went wrong.
|
|
53
|
+
|
|
54
|
+
#### Warnings
|
|
55
|
+
|
|
56
|
+
Warnings are events that indicate a potential issue in your application. They may not cause the application to crash, but they can lead to unexpected behavior or performance issues. Logging warnings can help you identify and fix potential problems before they become critical.
|
|
57
|
+
|
|
58
|
+
#### Important state changes
|
|
59
|
+
|
|
60
|
+
Logging important state changes in your application can help you understand the flow of your application and identify where issues are occurring. For example, you might want to log when a user logs in or out, when a database connection is established or closed, or when a critical function is called.
|
|
61
|
+
|
|
62
|
+
### What not to log
|
|
63
|
+
|
|
64
|
+
While you are developing or debugging your application, you might want to log everything to understand the flow of your application and its variables. However, there are some things that you should not log, especially in production environments for security and performance reasons. This includes:
|
|
65
|
+
|
|
66
|
+
#### Sensitive information
|
|
67
|
+
|
|
68
|
+
You should never log sensitive information such as passwords, credit card numbers, or personally identifiable information (PII). Logging sensitive information can lead to security breaches and legal issues. If you need to log sensitive information for debugging purposes, make sure to mask or obfuscate it before logging, and remove those logs before deploying to production.
|
|
69
|
+
|
|
70
|
+
#### 'Garbage' information
|
|
71
|
+
|
|
72
|
+
You might want to log everything during development, but in production, you should avoid logging 'garbage' information that is not useful for understanding the behavior of your application. This includes logging every single variable change or every single function call, which can lead to log bloat and make it difficult to find important information in the logs. Also, those logs can have a performance impact on your application, especially if they are logged synchronously.
|
|
73
|
+
|
|
74
|
+
## Log Levels
|
|
75
|
+
|
|
76
|
+
Log levels are used to indicate the severity of an event. They help you filter logs and focus on the most important information. I will show you the log levels based on my `MapleX` logger, but the concept is similar in other logging libraries.
|
|
77
|
+
|
|
78
|
+
### `TRACE`
|
|
79
|
+
|
|
80
|
+
The `TRACE` level is the lowest log level, and it is used for very detailed information that is typically only useful for debugging. It can include information about the flow of the application, variable values, and other low-level details when you are hunting for a specific hard-to-find issue. You should suppress `TRACE` logs in production environments, or they can cause a performance impact and make it difficult to find important information in the logs.
|
|
81
|
+
|
|
82
|
+
### `DEBUG`
|
|
83
|
+
|
|
84
|
+
The `DEBUG` level is used for information that is useful for coding and debugging, but not as detailed as `TRACE`. It can include information about the state of the application, such as when a function is called or when a variable is updated. You can use this level to understand 'how' and 'why' something is happening in your application. Like `TRACE`, you should suppress `DEBUG` logs in production environments to reduce the amount of logs and improve performance.
|
|
85
|
+
|
|
86
|
+
### `INFO`
|
|
87
|
+
|
|
88
|
+
The `INFO (INFORMATION)` level is used for general information about the application's operation. It can include information about the application's startup, shutdown, or other significant events that are not errors or warnings. `INFO` logs are standard 'milestones' that can be useful in production environments to understand the normal operation and the flow of the application. But you should avoid logging too much information at this level, and focus on logging important events that can help you understand the behavior of your application.
|
|
89
|
+
|
|
90
|
+
### `WARN`
|
|
91
|
+
|
|
92
|
+
The `WARN (WARNING)` level is used for events that indicate a potential issue in your application. They may not cause the application to crash, but they can lead to unexpected behavior or performance issues, such as deprecated API usage, or a failed connection to a third-party service. You can use this level to flag unexpected events or conditions that may require attention, but do not necessarily indicate a failure and the application can continue running. Logging warnings can help you identify and fix potential problems before they become critical.
|
|
93
|
+
|
|
94
|
+
### `ERROR`
|
|
95
|
+
|
|
96
|
+
The `ERROR` level is used for events that indicate a failure in your application. They indicate that something went wrong and the user probably got an error message, but the rest of the application can continue running. Logging errors can help you understand what went wrong, where it went wrong, and why it went wrong, so you can fix the issue and prevent it from happening again. You should also log the exception message and stack trace when an error occurs, as it can provide valuable information for debugging.
|
|
97
|
+
|
|
98
|
+
### `FATAL`
|
|
99
|
+
|
|
100
|
+
The `FATAL` (or `CRITICAL` might be used in some logging libraries) level is the highest log level, and it is used for events that indicate a critical failure in your application. They indicate that something went wrong, and the application cannot continue running or is in an unstable state. This level is used when the application encounters a situation that it cannot recover from, and is about to crash. Logging fatal errors can help you understand what went wrong, where it went wrong, and why it went wrong, so you can fix the issue and prevent it from happening again. You should also log the exception message and stack trace when a fatal error occurs, as it can provide valuable information for debugging.
|
|
101
|
+
|
|
102
|
+
### `NONE`
|
|
103
|
+
|
|
104
|
+
The `NONE` level is a special level in `MapleX` that not commonly exists in other logging libraries. It is used to bypass all the log level filters and always output the log, or suppress all other log levels if you set this in the logger configuration. This is useful for debugging in production environments, where you want to log a specific event, but don't want to log any other lower level events under the same logger configuration. For example, you can set the log level to `NONE` for a specific logger to always log suspicious events, while setting the log level to `WARNING` for other loggers to only log warnings and errors.
|
|
105
|
+
|
|
106
|
+
## Conclusion
|
|
107
|
+
|
|
108
|
+
Logging is an essential part of software development, and it can help you understand the behavior of your application, especially when things go wrong. By following the best practices for logging, you can ensure that your logs are useful, informative, and easy to understand. Remember to log important events, avoid logging sensitive information, and use log levels to filter logs and focus on the most important information.
|
|
109
|
+
|
|
110
|
+
If you master the art of logging, you can become a more effective developer and create applications that are easier to maintain and debug. You will never lose your important logs again when your application silently crashes and wipes out all the logs in the terminal. You can always review the logs in the log file, and understand what happened, where it happened, and why it happened.
|
|
111
|
+
|
|
112
|
+
So, start logging wisely, and use the power of logging to improve your development process and create better applications!
|
|
113
|
+
|
|
114
|
+
If you want to learn about how to use the `MapleX` logger, check out the [documentation](https://github.com/Ryuji-Hazama/MapleTree/blob/main/README.md) and the [Logger README](https://github.com/Ryuji-Hazama/MapleTree/blob/main/readmes/README_Logger.md) for more details and examples. `MapleX` is designed to be beginner-friendly and easy to use, so you can start logging effectively in your applications right away!
|
|
115
|
+
|
|
116
|
+
Happy logging, and may your logs always be informative and helpful!
|
|
@@ -210,6 +210,26 @@ print(jsonData)
|
|
|
210
210
|
{'data1': 'value1', 'data2': 67890}
|
|
211
211
|
```
|
|
212
212
|
|
|
213
|
+
## `readOrDefault()`
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
readOrDefault(
|
|
217
|
+
default: object,
|
|
218
|
+
*keys: str
|
|
219
|
+
) -> object:
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`x3.1.0` or later.
|
|
223
|
+
|
|
224
|
+
|Property|Required|Value|Version|
|
|
225
|
+
|--------|--------|-----|-------|
|
|
226
|
+
|**`default`**|\*|Default value to return if key does not exist|3.1.0|
|
|
227
|
+
|**`*keys`**||Dict keys|3.1.0|
|
|
228
|
+
|
|
229
|
+
This function reads a JSON file, which is specified at the class instance, and returns the data.
|
|
230
|
+
|
|
231
|
+
If the key(s) does not exist, it returns the `default` value.
|
|
232
|
+
|
|
213
233
|
### `write()`
|
|
214
234
|
|
|
215
235
|
```python
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: MapleX
|
|
3
|
-
Version: 3.1.
|
|
3
|
+
Version: 3.1.2
|
|
4
4
|
Summary: A Python library for simple logging, json file operations, Maple file format operations, and console color utilities.
|
|
5
5
|
Author: Ryuji Hazama
|
|
6
6
|
Project-URL: PyPI, https://pypi.org/project/MapleX/
|
|
@@ -216,7 +216,7 @@ class Formatter:
|
|
|
216
216
|
if '{callerLine}' in fileFormat:
|
|
217
217
|
|
|
218
218
|
callerLine = callerFrame.lineno
|
|
219
|
-
fileFormat = fileFormat.replace('{callerLine}', f'
|
|
219
|
+
fileFormat = fileFormat.replace('{callerLine}', f'{callerLine}')
|
|
220
220
|
|
|
221
221
|
alignStep = self.config.get(FILE_ALIGN_WIDTH, 1)
|
|
222
222
|
alignWidth = alignStep * (len(fileFormat) // alignStep + (1 if len(fileFormat) % alignStep != 0 else 0))
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
# Logging Best Practice
|
|
2
|
-
|
|
3
|
-
## What Is "Logging"
|
|
4
|
-
|
|
5
|
-
### The flight recorder of the application
|
|
6
|
-
|
|
7
|
-
When you are developing an application, you might experience that your application is crashing silently. Then you will insert a bunch of `print()` lines to determine where the application failed, and what was causing the error. That is a log, and those outputs are showing the exact path of your process, and help you understand what is working correctly, why, and where your code failed after the application halts.
|
|
8
|
-
|
|
9
|
-
However, those logs are disappearing when you close the output terminal, or the terminal was automatically closed by the application. That is why you need to output logs to a file like a flight recorder in a black box.
|
|
10
|
-
|
|
11
|
-
### Four W's of Logging
|
|
12
|
-
|
|
13
|
-
Every time the event occurs, the logger should capture the "Four W's" to get the details of the event.
|
|
14
|
-
|
|
15
|
-
- **When** — When the event occured.
|
|
16
|
-
- **Whrere** — Where the event happened.
|
|
17
|
-
- **What** — What was the event.
|
|
18
|
-
- **Weight** — How seriouc is the event?
|
|
19
|
-
|
|
20
|
-
### Logging vs `print()`
|
|
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
|
|
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
|