Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create logger function to instantiate a logger and return it.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import logging | ||
|
||
##################################### LOGS ##################################### | ||
# Initialize the logger and specify the level of logging. This will log "DEBUG" | ||
# and higher messages to file and log "WARNING" and higher messages to the console. | ||
|
||
def create_logger(): | ||
logging.basicConfig(level=logging.DEBUG, | ||
format='[%(levelname)s][%(name)s][%(asctime)s]: %(message)s', | ||
datefmt='%d-%m-%y-%H:%M:%S', | ||
filename='debug.log', | ||
filemode='a') | ||
|
||
# Define a "handler" which writes "WARNING" messages or higher to the "sys.stderr". | ||
console = logging.StreamHandler() | ||
console.setLevel(logging.WARNING) | ||
|
||
# Set a format which is simpler for console messages. | ||
formatter = logging.Formatter('[%(levelname)s][%(asctime)s]: %(message)s', datefmt='%H:%M:%S') | ||
|
||
# Tell the console "handler" to use this format. | ||
console.setFormatter(formatter) | ||
|
||
# Add the "handler" to the "root logger". | ||
log = logging.getLogger(__name__) | ||
log.addHandler(console) | ||
|
||
return log | ||
|
||
# Example Usage | ||
# | ||
# import tkintermd.log as logg | ||
# | ||
# logger = log.create_logger() | ||
# logger.debug("DEBUG") | ||
# logger.info("INFO") | ||
# logger.exception("EXCEPTION") | ||
# logger.warning("WARNING") | ||
# logger.error("ERROR") | ||
# logger.critical("CRITICAL") | ||
|
||
################################################################################ |