-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_config.py
More file actions
112 lines (90 loc) · 4.03 KB
/
logger_config.py
File metadata and controls
112 lines (90 loc) · 4.03 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
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
import logging
import os
import sys
from datetime import datetime
def get_app_data_dir():
"""Get the appropriate app data directory for each platform."""
app_name = 'Facebook Scheduler'
if sys.platform == 'win32': # Windows
base_dir = os.environ.get('LOCALAPPDATA')
path = os.path.join(base_dir, app_name)
print(f"Windows AppData path: {path}") # Debug print
return path
elif sys.platform == 'darwin': # macOS
base_dir = os.path.expanduser('~/Library/Application Support')
return os.path.join(base_dir, app_name)
else: # Linux or other Unix
base_dir = os.path.expanduser('~/.local/share')
return os.path.join(base_dir, app_name)
def setup_logging():
"""Configure logging for the application."""
# Check if logging is already configured
if getattr(setup_logging, '_initialized', False):
return getattr(setup_logging, '_log_file', None)
try:
# Get platform-specific app data directory
app_data_dir = get_app_data_dir()
log_dir = os.path.join(app_data_dir, 'logs')
# Create directories if they don't exist
os.makedirs(log_dir, exist_ok=True)
# Create log file with date
current_date = datetime.now().strftime('%Y%m%d')
log_file = os.path.join(log_dir, f'app_{current_date}.log')
# Configure logging
formatter = logging.Formatter(
'%(asctime)s [%(name)s] %(levelname)s: %(message)s'
)
# File handler
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
console_handler.setLevel(logging.DEBUG)
# Root logger
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
# Remove any existing handlers
root_logger.handlers = []
# Add handlers
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
# Add these lines to silence websockets messages
logging.getLogger('websockets').setLevel(logging.ERROR) # Only show errors
logging.getLogger('websockets.client').setLevel(logging.ERROR) # Specifically silence client messages
logging.getLogger('websockets.server').setLevel(logging.ERROR) # Optionally silence server messages too
# Silence Selenium connection messages
logging.getLogger('selenium.webdriver.remote.remote_connection').setLevel(logging.ERROR)
logging.getLogger('urllib3.connectionpool').setLevel(logging.ERROR) # Also silence related urllib3 messages
# Mark as initialized and store log file path
setattr(setup_logging, '_initialized', True)
setattr(setup_logging, '_log_file', log_file)
return log_file
except Exception as e:
print(f"Error setting up logging: {str(e)}")
import traceback
traceback.print_exc()
return None
def get_logger(name):
"""Get a logger with the specified name."""
return logging.getLogger(name)
# Add this test function
def test_logging():
"""Test if logging is working."""
log_file = setup_logging()
if log_file:
logger = logging.getLogger('test')
logger.info("Test log message")
print(f"Test message should be written to: {log_file}")
# Verify file exists and contains content
if os.path.exists(log_file):
with open(log_file, 'r') as f:
content = f.read()
print(f"Log file contents:\n{content}")
else:
print(f"Log file does not exist: {log_file}")
else:
print("Logging setup failed")
if __name__ == "__main__":
test_logging()