Skip to content

Commit afd3d1c

Browse files
committed
Create logger function to instantiate a logger and return it.
1 parent 69ddbdf commit afd3d1c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tkintermd/log.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import logging
2+
3+
##################################### LOGS #####################################
4+
# Initialize the logger and specify the level of logging. This will log "DEBUG"
5+
# and higher messages to file and log "WARNING" and higher messages to the console.
6+
7+
def create_logger():
8+
logging.basicConfig(level=logging.DEBUG,
9+
format='[%(levelname)s][%(name)s][%(asctime)s]: %(message)s',
10+
datefmt='%d-%m-%y-%H:%M:%S',
11+
filename='debug.log',
12+
filemode='a')
13+
14+
# Define a "handler" which writes "WARNING" messages or higher to the "sys.stderr".
15+
console = logging.StreamHandler()
16+
console.setLevel(logging.WARNING)
17+
18+
# Set a format which is simpler for console messages.
19+
formatter = logging.Formatter('[%(levelname)s][%(asctime)s]: %(message)s', datefmt='%H:%M:%S')
20+
21+
# Tell the console "handler" to use this format.
22+
console.setFormatter(formatter)
23+
24+
# Add the "handler" to the "root logger".
25+
log = logging.getLogger(__name__)
26+
log.addHandler(console)
27+
28+
return log
29+
30+
# Example Usage
31+
#
32+
# import tkintermd.log as logg
33+
#
34+
# logger = log.create_logger()
35+
# logger.debug("DEBUG")
36+
# logger.info("INFO")
37+
# logger.exception("EXCEPTION")
38+
# logger.warning("WARNING")
39+
# logger.error("ERROR")
40+
# logger.critical("CRITICAL")
41+
42+
################################################################################

0 commit comments

Comments
 (0)