-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_if_working.py
147 lines (138 loc) · 4.6 KB
/
test_if_working.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from indented_logger import setup_logging
import logging
# Enable hierarchy-based indentation
setup_logging(
level=logging.INFO,
include_func=True,
include_module=False,
func_module_format='{funcName}',
use_logger_hierarchy=True
)
# Create hierarchical loggers
logger_root = logging.getLogger('myapp')
logger_submodule = logging.getLogger('myapp.submodule')
def main():
logger_root.info('Starting application')
process_submodule()
logger_root.info('Application finished')
def process_submodule():
logger_submodule.info('Processing submodule task 1 ')
logger_submodule.info('Processing submodule task 2 ')
if __name__ == '__main__':
main()
# import logging
# import threading
# from indented_logger import setup_logging, log_indent
#
# # Configure logging
# setup_logging(
# level=logging.DEBUG,
# include_func=True,
# truncate_messages=False,
# min_func_name_col=100,
# use_logger_hierarchy=True,
# indent_spaces=4
# )
#
# # Create hierarchical loggers
# logger_root = logging.getLogger('app')
# logger_module = logging.getLogger('app.module')
# logger_submodule = logging.getLogger('app.module.submodule')
#
# # Example 1: Basic logging with hierarchy-based indentation
# def basic_logging_example():
# logger_root.info('Starting the application')
# logger_module.info('Initializing module')
# logger_submodule.info('Running submodule tasks')
# logger_module.info('Module completed')
# logger_root.info('Application finished')
#
# # Example 2: Using manual indentation with 'lvl' parameter
# def manual_indentation_example():
# logger = logging.getLogger('manual')
# logger.info('Manual indentation level 0', extra={'lvl': 0})
# logger.info('Manual indentation level 1', extra={'lvl': 1})
# logger.info('Manual indentation level 2', extra={'lvl': 2})
# logger.info('Back to level 1', extra={'lvl': 1})
# logger.info('Back to level 0', extra={'lvl': 0})
#
# # Example 3: Using the @log_indent decorator
# @log_indent
# def decorated_function():
# logger = logging.getLogger('decorated')
# logger.info('Inside decorated function')
# nested_function()
#
# @log_indent
# def nested_function():
# logger = logging.getLogger('decorated.nested')
# logger.info('Inside nested function')
#
# # Example 4: Combining hierarchy, decorators, and manual indentation
# @log_indent
# def complex_example():
# logger = logging.getLogger('app.complex')
# logger.info('Starting complex operation')
# logger.info('Manual indent inside decorator', extra={'lvl': 1})
# sub_operation()
# logger.info('Complex operation finished')
#
# @log_indent
# def sub_operation():
# logger = logging.getLogger('app.complex.sub')
# logger.info('Sub-operation in progress')
#
# # Example 5: Demonstrating message truncation
# def message_truncation_example():
# # Reconfigure logging with message truncation enabled
# setup_logging(
# level=logging.DEBUG,
# include_func=True,
# truncate_messages=True,
# min_func_name_col=100,
# use_logger_hierarchy=True,
# indent_spaces=4
# )
# logger = logging.getLogger('truncate')
# long_message = 'This is a very long log message that will be truncated to a maximum length for display purposes'
# logger.info(long_message)
#
# # Example 6: Multi-threading to demonstrate thread safety
# def threading_example():
# def worker(thread_id):
# logger = logging.getLogger(f'app.thread{thread_id}')
# logger.info(f'Thread {thread_id} starting work')
# @log_indent
# def thread_task():
# logger.info(f'Thread {thread_id} is working')
# thread_task()
# logger.info(f'Thread {thread_id} finished work')
#
# threads = []
# for i in range(2):
# t = threading.Thread(target=worker, args=(i,))
# threads.append(t)
# t.start()
#
# for t in threads:
# t.join()
#
# # Run examples
# if __name__ == '__main__':
# print("\n--- Example 1: Basic Logging with Hierarchy-based Indentation ---\n")
# basic_logging_example()
#
# print("\n--- Example 2: Manual Indentation with 'lvl' Parameter ---\n")
# manual_indentation_example()
#
# print("\n--- Example 3: Using the @log_indent Decorator ---\n")
# decorated_function()
#
# print("\n--- Example 4: Combining Hierarchy, Decorators, and Manual Indentation ---\n")
# complex_example()
#
# print("\n--- Example 5: Demonstrating Message Truncation ---\n")
# message_truncation_example()
#
# print("\n--- Example 6: Multi-threading to Demonstrate Thread Safety ---\n")
# threading_example()