-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
71 lines (55 loc) · 2.03 KB
/
logger.py
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
# import logging
# from logging.handlers import TimedRotatingFileHandler
# from datetime import datetime, timedelta
# import pytz
# # Set up logging
# logging.basicConfig(
# level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
# )
# # Set IST timezone
# ist_timezone = pytz.timezone("Asia/Kolkata")
# # Create a TimedRotatingFileHandler to rotate logs daily
# handler = TimedRotatingFileHandler(
# "app.log", when="midnight", interval=1, backupCount=7, encoding="utf-8"
# )
# handler.suffix = "%Y%m%d.log"
# handler.setFormatter(
# logging.Formatter(
# "%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
# )
# )
# handler.rolloverAt = datetime.now(ist_timezone) + timedelta(
# seconds=1
# ) # Ensure the first rollover happens at midnight IST
# # Add the handler to the logger
# logger = logging.getLogger(__name__)
# logger.addHandler(handler)
# logger.setLevel(logging.DEBUG)
# if __name__ == "__main__":
# logger.debug("Logging initialized.")
import logging
from datetime import datetime
import pytz
# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# Set IST timezone
ist_timezone = pytz.timezone("Asia/Kolkata")
# Create a formatter for console logging
console_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
# Create a handler for console logging
console_handler = logging.StreamHandler()
console_handler.setFormatter(console_formatter)
# Add the console handler to the logger
logger = logging.getLogger(__name__)
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)
# Function to set IST timezone for the timestamp
def ist_time(*args):
utc_time = datetime.utcnow()
utc_time = pytz.utc.localize(utc_time)
ist_time = utc_time.astimezone(ist_timezone)
return ist_time.timetuple()
# Set the custom timestamp function for the formatter
console_formatter.converter = ist_time
if __name__ == "__main__":
logger.info("Google search successful.")