-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogging_config.py
More file actions
69 lines (55 loc) · 2 KB
/
Copy pathlogging_config.py
File metadata and controls
69 lines (55 loc) · 2 KB
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
import logging
import os
import yaml
def load_config(config_file="config.yaml"):
"""
Load the configuration settings from a YAML file.
Args:
config_file (str): Path to the YAML configuration file.
Returns:
dict: The configuration settings as a dictionary.
"""
with open(config_file, 'r') as file:
config = yaml.safe_load(file)
return config
def setup_logging(config):
"""
Set up the logging configuration based on the settings in the config file.
Args:
config (dict): Configuration settings loaded from the YAML file.
Returns:
logging.Logger: Configured logger instance.
"""
# Extract logging settings from the configuration
log_level = config['logging'].get('level', 'INFO').upper()
log_file = config['logging'].get('log_file', 'refactor_earth.log')
console_output = config['logging'].get('console_output', True)
# Create log directory if it doesn't exist
log_dir = os.path.dirname(log_file)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Configure logging
logging.basicConfig(
level=getattr(logging, log_level),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler() if console_output else logging.NullHandler()
]
)
# Create a logger instance
logger = logging.getLogger('RefactorEarth')
logger.info('Logging setup complete.')
return logger
# Example usage within another script
if __name__ == "__main__":
# Load configuration from the config.yaml file
config = load_config()
# Set up logging based on the configuration
logger = setup_logging(config)
# Example logging messages
logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")