SwiftGUI_Logging 0.1.0__tar.gz → 0.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.
@@ -0,0 +1,132 @@
1
+ Metadata-Version: 2.4
2
+ Name: SwiftGUI_Logging
3
+ Version: 0.1.2
4
+ Summary: A collection of helpful logging-functionality based on the logging package
5
+ License-Expression: Apache-2.0
6
+ License-File: LICENSE
7
+ Author: Eric aka CheesecakeTV
8
+ Author-email: cheesecaketv53+pypi@gmail.com
9
+ Requires-Python: >=3.10
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Operating System :: OS Independent
12
+ Project-URL: Documentation, https://github.com/CheesecakeTV/SwiftGUI-Docs
13
+ Project-URL: Repository, https://github.com/CheesecakeTV/SwiftGUI-Logging
14
+ Project-URL: issues, https://github.com/CheesecakeTV/SwiftGUI/issues
15
+ Description-Content-Type: text/markdown
16
+
17
+
18
+ # SwiftGUI-Logging: Motivation
19
+ A small package ment to extend logging to better fit actual applications.
20
+
21
+ Before getting into the logging-package, I usually implemented
22
+ something like this (but more complicated):
23
+ ```py
24
+ def main():
25
+ ...
26
+
27
+ if __name__ == "__main__":
28
+ try:
29
+ main() # Run the main program
30
+ except Exception as ex: # An exception occured
31
+ with open("Crashlog.txt", "w") as f: # Save exception to file
32
+ f.write(str(ex))
33
+ ```
34
+ If `main()` causes an exception, the file `Crashlog.txt` is created
35
+ containing information about the exception.
36
+ That means, only "the interesting logs" take up storage space.
37
+
38
+ Unfortunately, you can't implement such a functionality using the
39
+ logging-package.
40
+
41
+ Until now.
42
+
43
+ SwiftGUI-Logging provides a very easy way to set up crashlogs that
44
+ are fully compatible with the logging-package.
45
+
46
+ # Basic usage
47
+ ## Installation
48
+ Install the package by running this on your terminal:
49
+ ```bash
50
+ pip install SwiftGUI_Logging
51
+ ```
52
+
53
+ ## Crashlogging to a file
54
+ Simply call `SwiftGUI_Logging.Configs.exceptions_to_file(filepath)`
55
+ to set up the crashlogger.
56
+
57
+ That's all.
58
+
59
+ Example:
60
+ ```py
61
+ import time
62
+ import SwiftGUI_Logging as sgl
63
+ import logging
64
+
65
+ def main():
66
+ for i in range(10):
67
+ logging.debug(f"Test {i}")
68
+ print(i)
69
+ time.sleep(0.25)
70
+
71
+ logging.info("Crashing the program now")
72
+
73
+ 1 / 0 # Cause a ZeroDivisionError, which crashes the program
74
+
75
+ if __name__ == '__main__':
76
+ sgl.Configs.exceptions_to_file("Crashlogs/Crash.log") # Set up the crash-log
77
+ main() # Execute program
78
+ ```
79
+
80
+ You'll find that the directory `Crashlogs` was created.
81
+ After the program executes, the directory contains a file like
82
+ `Crash_2026-02-24_16-33-52.log`.
83
+
84
+ As you can see, the time of the crash was inserted into the
85
+ filename, so that multiple crashlogs don't overwrite each other.
86
+
87
+ The file contains the most recent log-entries (from `logging.debug`)
88
+ and the exception with full traceback:
89
+ ```log
90
+ 2026-02-24 16:33:49,940 - root - DEBUG - Test 0
91
+ 2026-02-24 16:33:50,197 - root - DEBUG - Test 1
92
+ 2026-02-24 16:33:50,451 - root - DEBUG - Test 2
93
+ 2026-02-24 16:33:50,704 - root - DEBUG - Test 3
94
+ 2026-02-24 16:33:50,958 - root - DEBUG - Test 4
95
+ 2026-02-24 16:33:51,214 - root - DEBUG - Test 5
96
+ 2026-02-24 16:33:51,469 - root - DEBUG - Test 6
97
+ 2026-02-24 16:33:51,723 - root - DEBUG - Test 7
98
+ 2026-02-24 16:33:51,979 - root - DEBUG - Test 8
99
+ 2026-02-24 16:33:52,235 - root - DEBUG - Test 9
100
+ 2026-02-24 16:33:52,490 - root - INFO - Crashing the program now
101
+ 2026-02-24 16:33:52,490 - root - ERROR - Traceback (most recent call last):
102
+ File "C:\Users\chees\PycharmProjects\SwiftGUI-Logging\tests\test.py", line 17, in <module>
103
+ main() # Execute program
104
+ File "C:\Users\chees\PycharmProjects\SwiftGUI-Logging\tests\test.py", line 13, in main
105
+ 1 / 0 # Cause a ZeroDivisionError, which crashes the program
106
+ ZeroDivisionError: division by zero
107
+ ```
108
+
109
+ `sgl.Configs.exceptions_to_file` can do a bit more, but for most programs,
110
+ the default configuration is fine.
111
+
112
+ ## Other functionality
113
+ `SwiftGUI_Logging` provides some other functionality, but these
114
+ aren't nearly as important as the `exceptions_to_file`-function.
115
+
116
+ A detailed documentation will follow.
117
+
118
+ # SwiftGUI
119
+ This package was written as an addition to
120
+ my Python GUI-package `SwiftGUI`:
121
+ https://github.com/CheesecakeTV/SwiftGUI
122
+
123
+ Since `SwiftGUI_Logging` itself has nothing to to with GUIs,
124
+ it is its own package.
125
+
126
+ Consider checking out `SwiftGUI` if you want to easily create
127
+ user-interfaces for python.
128
+ If you already know the package `PySimpleGUI`, you'll learn the
129
+ basics of`SwiftGUI` with little to no effort.
130
+
131
+
132
+
@@ -0,0 +1,115 @@
1
+
2
+ # SwiftGUI-Logging: Motivation
3
+ A small package ment to extend logging to better fit actual applications.
4
+
5
+ Before getting into the logging-package, I usually implemented
6
+ something like this (but more complicated):
7
+ ```py
8
+ def main():
9
+ ...
10
+
11
+ if __name__ == "__main__":
12
+ try:
13
+ main() # Run the main program
14
+ except Exception as ex: # An exception occured
15
+ with open("Crashlog.txt", "w") as f: # Save exception to file
16
+ f.write(str(ex))
17
+ ```
18
+ If `main()` causes an exception, the file `Crashlog.txt` is created
19
+ containing information about the exception.
20
+ That means, only "the interesting logs" take up storage space.
21
+
22
+ Unfortunately, you can't implement such a functionality using the
23
+ logging-package.
24
+
25
+ Until now.
26
+
27
+ SwiftGUI-Logging provides a very easy way to set up crashlogs that
28
+ are fully compatible with the logging-package.
29
+
30
+ # Basic usage
31
+ ## Installation
32
+ Install the package by running this on your terminal:
33
+ ```bash
34
+ pip install SwiftGUI_Logging
35
+ ```
36
+
37
+ ## Crashlogging to a file
38
+ Simply call `SwiftGUI_Logging.Configs.exceptions_to_file(filepath)`
39
+ to set up the crashlogger.
40
+
41
+ That's all.
42
+
43
+ Example:
44
+ ```py
45
+ import time
46
+ import SwiftGUI_Logging as sgl
47
+ import logging
48
+
49
+ def main():
50
+ for i in range(10):
51
+ logging.debug(f"Test {i}")
52
+ print(i)
53
+ time.sleep(0.25)
54
+
55
+ logging.info("Crashing the program now")
56
+
57
+ 1 / 0 # Cause a ZeroDivisionError, which crashes the program
58
+
59
+ if __name__ == '__main__':
60
+ sgl.Configs.exceptions_to_file("Crashlogs/Crash.log") # Set up the crash-log
61
+ main() # Execute program
62
+ ```
63
+
64
+ You'll find that the directory `Crashlogs` was created.
65
+ After the program executes, the directory contains a file like
66
+ `Crash_2026-02-24_16-33-52.log`.
67
+
68
+ As you can see, the time of the crash was inserted into the
69
+ filename, so that multiple crashlogs don't overwrite each other.
70
+
71
+ The file contains the most recent log-entries (from `logging.debug`)
72
+ and the exception with full traceback:
73
+ ```log
74
+ 2026-02-24 16:33:49,940 - root - DEBUG - Test 0
75
+ 2026-02-24 16:33:50,197 - root - DEBUG - Test 1
76
+ 2026-02-24 16:33:50,451 - root - DEBUG - Test 2
77
+ 2026-02-24 16:33:50,704 - root - DEBUG - Test 3
78
+ 2026-02-24 16:33:50,958 - root - DEBUG - Test 4
79
+ 2026-02-24 16:33:51,214 - root - DEBUG - Test 5
80
+ 2026-02-24 16:33:51,469 - root - DEBUG - Test 6
81
+ 2026-02-24 16:33:51,723 - root - DEBUG - Test 7
82
+ 2026-02-24 16:33:51,979 - root - DEBUG - Test 8
83
+ 2026-02-24 16:33:52,235 - root - DEBUG - Test 9
84
+ 2026-02-24 16:33:52,490 - root - INFO - Crashing the program now
85
+ 2026-02-24 16:33:52,490 - root - ERROR - Traceback (most recent call last):
86
+ File "C:\Users\chees\PycharmProjects\SwiftGUI-Logging\tests\test.py", line 17, in <module>
87
+ main() # Execute program
88
+ File "C:\Users\chees\PycharmProjects\SwiftGUI-Logging\tests\test.py", line 13, in main
89
+ 1 / 0 # Cause a ZeroDivisionError, which crashes the program
90
+ ZeroDivisionError: division by zero
91
+ ```
92
+
93
+ `sgl.Configs.exceptions_to_file` can do a bit more, but for most programs,
94
+ the default configuration is fine.
95
+
96
+ ## Other functionality
97
+ `SwiftGUI_Logging` provides some other functionality, but these
98
+ aren't nearly as important as the `exceptions_to_file`-function.
99
+
100
+ A detailed documentation will follow.
101
+
102
+ # SwiftGUI
103
+ This package was written as an addition to
104
+ my Python GUI-package `SwiftGUI`:
105
+ https://github.com/CheesecakeTV/SwiftGUI
106
+
107
+ Since `SwiftGUI_Logging` itself has nothing to to with GUIs,
108
+ it is its own package.
109
+
110
+ Consider checking out `SwiftGUI` if you want to easily create
111
+ user-interfaces for python.
112
+ If you already know the package `PySimpleGUI`, you'll learn the
113
+ basics of`SwiftGUI` with little to no effort.
114
+
115
+
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "SwiftGUI_Logging"
3
- version = "0.1.0"
3
+ version = "0.1.2"
4
4
  packages = [
5
5
  { include = "SwiftGUI_Logging", from = "src" }
6
6
  ]
@@ -37,8 +37,6 @@ def exceptions_to_file(
37
37
  filepath = Path(filepath)
38
38
  filepath.parent.mkdir(parents=True, exist_ok=True)
39
39
 
40
- filepath = filepath.parent / (filepath.stem + dt.now().strftime(datetime_format) + filepath.suffix)
41
-
42
40
  if isinstance(logger, str):
43
41
  logger = logging.getLogger(logger)
44
42
 
@@ -49,21 +47,29 @@ def exceptions_to_file(
49
47
  stream_handler.setFormatter(
50
48
  logging.Formatter(formatter_format)
51
49
  )
52
- buffer_handler = sgl.MemoryHandlerRotatingBuffer(buffer_size, trigger_level, target=stream_handler)
53
-
54
- logger.addHandler(buffer_handler)
55
50
 
56
51
  def exception_occured(*_):
52
+ nonlocal stream
57
53
  stream.seek(0)
54
+
58
55
  if not stream.read(1):
59
56
  # Nothing to report
60
- print("Nothing to report")
61
57
  return
62
58
 
63
- with open(filepath, "w") as f:
59
+ # Add current datetime to filename
60
+ actual_filepath = filepath.parent / (filepath.stem + dt.now().strftime(datetime_format) + filepath.suffix)
61
+
62
+ with open(actual_filepath, "a") as f:
64
63
  stream.seek(0)
65
- #f.write(stream.read())
66
64
  shutil.copyfileobj(stream, f)
67
65
 
66
+ stream.close()
67
+ stream = io.StringIO() # "Clear" the buffer
68
+ stream_handler.setStream(stream)
69
+
70
+ buffer_handler = sgl.MemoryHandlerRotatingBuffer(buffer_size, trigger_level, target=stream_handler, call_after_flushing=exception_occured)
71
+
72
+ logger.addHandler(buffer_handler)
73
+
68
74
  sgl.reroute_exceptions(logger, reraise=reraise, loglevel=trigger_level, pass_text_to_function=exception_occured)
69
75
 
@@ -1,9 +1,10 @@
1
1
  import logging.handlers
2
+ from typing import Callable
2
3
 
3
4
 
4
5
  class MemoryHandlerRotatingBuffer(logging.handlers.MemoryHandler):
5
6
 
6
- def __init__(self, capacity, flushLevel=logging.ERROR, target=None):
7
+ def __init__(self, capacity, flushLevel=logging.ERROR, target=None, call_after_flushing: Callable = None):
7
8
  """
8
9
  This handler saves the last n records.
9
10
  Following records replace the oldest ones.
@@ -14,9 +15,11 @@ class MemoryHandlerRotatingBuffer(logging.handlers.MemoryHandler):
14
15
 
15
16
  :param capacity: How many records to buffer before the oldest ones get deleted
16
17
  :param flushLevel: At which level of record the whole buffer is passed to the target-handler
18
+ :param call_after_flushing: Call this function after passing the entries to the target
17
19
  :param target: Handler to receive all records if necessary
18
20
  """
19
21
  super().__init__(capacity, flushLevel, target, flushOnClose=False)
22
+ self.call_after_flushing = call_after_flushing if call_after_flushing else lambda *_:_
20
23
 
21
24
  def shouldFlush(self, record):
22
25
  """
@@ -29,3 +32,8 @@ class MemoryHandlerRotatingBuffer(logging.handlers.MemoryHandler):
29
32
 
30
33
  return record.levelno >= self.flushLevel
31
34
 
35
+ def flush(self):
36
+ super().flush()
37
+ self.call_after_flushing()
38
+
39
+
@@ -1,21 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: SwiftGUI_Logging
3
- Version: 0.1.0
4
- Summary: A collection of helpful logging-functionality based on the logging package
5
- License-Expression: Apache-2.0
6
- License-File: LICENSE
7
- Author: Eric aka CheesecakeTV
8
- Author-email: cheesecaketv53+pypi@gmail.com
9
- Requires-Python: >=3.10
10
- Classifier: Programming Language :: Python :: 3
11
- Classifier: Operating System :: OS Independent
12
- Project-URL: Documentation, https://github.com/CheesecakeTV/SwiftGUI-Docs
13
- Project-URL: Repository, https://github.com/CheesecakeTV/SwiftGUI-Logging
14
- Project-URL: issues, https://github.com/CheesecakeTV/SwiftGUI/issues
15
- Description-Content-Type: text/markdown
16
-
17
-
18
- # SwiftGUI-Logging
19
-
20
- Still WIP
21
-
@@ -1,4 +0,0 @@
1
-
2
- # SwiftGUI-Logging
3
-
4
- Still WIP