-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppLogger.py
68 lines (59 loc) · 2.01 KB
/
AppLogger.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
# AppLogger : The application logger for python
# Useful in auditing actions/variables and storing everything to a log file.
# Git repo: https://github.com/codarrenvelvindron/AppLogger-python
# By Codarren Velvindron
# Contact: [email protected]
# 25/02/2021
# Licence: MIT
import os
from datetime import datetime
class Logger:
__logname = ''
__folderpath = ''
__logpath = ''
__gen_date_v = ''
__timestamp = ''
__separator = ''
__extension = ''
def __init__(self, logname, separator = ' ', extension = '.log'):
""" Init for class """
self.__logname = logname
self.__separator = separator
self.__extension = extension
def __make_folder(self):
cwd = os.getcwd()
dirname = self.__logname + "_logs"
self.__folderpath = os.path.join(cwd, dirname)
try:
os.makedirs (self.__folderpath,exist_ok = True)
except OSError:
pass
def __gen_date(self):
""" To generate date for logfile """
now = datetime.now()
format = "%d%m%Y"
self.__gen_date_v = now.strftime(format)
def __gen_log(self):
""" Create log with with date and extension """
self.__gen_date()
logdate = self.__gen_date_v
logext = self.__extension
current_logname = str(self.__logname) + "_" + str(logdate) + logext
self.__logpath = os.path.join(self.__folderpath, current_logname)
if not os.path.exists(self.__logpath):
f = open (self.__logpath, "w")
f.close()
def __gen_timestamp(self):
self.__timestamp = datetime.now()
def write(self, action='action', data='data'):
self.__make_folder()
self.__gen_log()
self.__gen_timestamp()
timestamp = str(self.__timestamp)
data = str(data)
action = str(action)
sep = self.__separator
entry = timestamp + sep + action + sep + data
f = open (self.__logpath, "a")
f.write('{}\n'.format(entry))
f.close