Skip to content

Commit

Permalink
Proper logging
Browse files Browse the repository at this point in the history
  • Loading branch information
F33RNI committed Feb 28, 2023
1 parent ce962ec commit 28b3145
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/*.zip
/test.py
/logs/
33 changes: 29 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
"""

import argparse
import datetime
import json
import locale
import logging
import os
import signal
import sys

import psutil

Expand All @@ -32,19 +35,41 @@
# Logging level (INFO for debug, WARN for release)
LOGGING_LEVEL = logging.INFO

# JSON Files
# Files and directories
SETTINGS_FILE = 'settings.json'
MESSAGES_FILE = 'messages.json'
LOGS_DIR = 'logs'


def logging_setup():
"""
Sets up logging format and level
:return:
"""
logging.basicConfig(encoding='utf-8', format='%(asctime)s %(levelname)-8s %(message)s',
level=LOGGING_LEVEL,
datefmt='%Y-%m-%d %H:%M:%S')
# Create logs directory
if not os.path.exists(LOGS_DIR):
os.makedirs(LOGS_DIR)

# Create logs formatter
log_formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

# Setup logging into file
file_handler = logging.FileHandler(os.path.join(LOGS_DIR,
datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + '.log'),
encoding='utf-8')
file_handler.setFormatter(log_formatter)

# Setup logging into console
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(log_formatter)

# Add all handlers and setup level
root_logger = logging.getLogger()
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
root_logger.setLevel(LOGGING_LEVEL)

# Log test message
logging.info('logging setup is complete')


Expand Down

0 comments on commit 28b3145

Please sign in to comment.