-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
88 lines (70 loc) · 2.82 KB
/
common.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: User
#
# Created: 04-11-2018
# Copyright: (c) User 2018
# Licence: <your licence>
#-------------------------------------------------------------------------------
# StdLib
import os
import logging
import logging.handlers
import datetime
# Remote libraries
# local
# logging setup
def setup_logging(log_file_path, timestamp_filename=True, max_log_size=104857600,console_level=logging.DEBUG):
"""Setup logging (Before running any other code)
http://inventwithpython.com/blog/2012/04/06/stop-using-print-for-debugging-a-5-minute-quickstart-guide-to-pythons-logging-module/
"""
assert( len(log_file_path) > 1 )
assert( type(log_file_path) == type("") )
global logger
# Make sure output dir(s) exists
log_file_folder = os.path.dirname(log_file_path)
if log_file_folder is not None:
if not os.path.exists(log_file_folder):
os.makedirs(log_file_folder)
# Add timetamp for filename if needed
if timestamp_filename:
# http://stackoverflow.com/questions/8472413/add-utc-time-to-filename-python
# '2015-06-30-13.44.15'
timestamp_string = datetime.datetime.utcnow().strftime("%Y-%m-%d %H.%M.%S%Z")
# Full log
log_file_path = add_timestamp_to_log_filename(log_file_path,timestamp_string)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# 2018-10-02 18:16:15,229 - INFO - f.auto_failover.py - ln.117 - Logging started.
formatter = logging.Formatter("%(asctime)s - %(levelname)s - f.%(filename)s - ln.%(lineno)d - %(message)s")
# File 1, log everything
# https://docs.python.org/2/library/logging.handlers.html
# Rollover occurs whenever the current log file is nearly maxBytes in length; if either of maxBytes or backupCount is zero, rollover never occurs.
fh = logging.handlers.RotatingFileHandler(
filename=log_file_path,
# https://en.wikipedia.org/wiki/Binary_prefix
# 104857600 100MiB
maxBytes=max_log_size,
backupCount=2,# Two files worth of logs should be plenty.
)
fh.setLevel(logging.DEBUG)# Store all messages in the logfile so we can debug problems
fh.setFormatter(formatter)
logger.addHandler(fh)
# Console output
ch = logging.StreamHandler()
ch.setLevel(console_level)
ch.setFormatter(formatter)
logger.addHandler(ch)
logging.info("Logging started. log_file_path = {0!r}".format(log_file_path))
return logger
def add_timestamp_to_log_filename(log_file_path, timestamp_string):
"""Insert a string before a file extention"""
base, ext = os.path.splitext(log_file_path)
return base+"_"+timestamp_string+ext
# /logging setup
def main():
pass
if __name__ == '__main__':
main()