Skip to content

Commit 938bb7c

Browse files
committed
Improvements
- default value of loggerName = __name__ - Better spacing in log statements - Added a [!] for warning logs
1 parent c8e2f2f commit 938bb7c

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/generalpy/custom_logging.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ def format(self, record: logging.LogRecord) -> str:
5252
class CustomLogging:
5353
""" Class to handle logging in easy way """
5454

55+
# compactFormat = f"[%(asctime)s] %(levelname)-8s %(module)-20s %(message)s"
56+
compactFormat = f"%(module)-20s (%(lineno)-4d): {' ' * 10} %(message)s"
57+
fullFormat = f"[%(asctime)s] [%(levelname)-8s: %(module)-20s-%(lineno)-4d]: {' ' * 10} %(message)s {' ' * 50} [%(threadName)s]"
58+
5559
def __init__(
5660
self,
57-
loggerName: str,
61+
loggerName: str | None = None,
5862
loggingLevel: int = logging.INFO,
5963
allLogsFilePath: str | None = None,
6064
errorLogsFilePath: str | None = None,
@@ -75,7 +79,7 @@ def __init__(
7579
- `initialMsg` : Initial message to set as soon as the logger initiates for the first time
7680
"""
7781
# Args
78-
self.loggerName = loggerName
82+
self.loggerName = loggerName if loggerName else __name__
7983
self.loggingLevel = loggingLevel
8084
self.allLogsFilePath = allLogsFilePath
8185
self.errorLogsFilePath = errorLogsFilePath
@@ -98,9 +102,10 @@ def __init__(
98102
self._initiate_file_logging(
99103
self.errorLogsFilePath, logging.ERROR
100104
)
101-
self.raw_logging(
102-
initialMsg, True, True
103-
)
105+
if initialMsg:
106+
self.raw_logging(
107+
initialMsg, True, True
108+
)
104109

105110
def _initiate_logger(self):
106111
""" Returns `Logger` after initiating it """
@@ -111,26 +116,24 @@ def _initiate_logger(self):
111116

112117
def _get_compact_formatter(self):
113118
""" Returns compact `Formatter` after initiating it for all levels """
114-
s1 = " " * 10
115-
s2 = " " * 50
116119
return LevelFormatter(
117120
{
118-
logging.DEBUG: f'~ %(module)s (%(lineno)d): {s1} %(message)s',
119-
logging.INFO: f'> %(module)s (%(lineno)d): {s1} %(message)s',
120-
logging.ERROR: f'[x] %(module)s (%(lineno)d): {s1} %(message)s',
121+
logging.DEBUG: f'[~] {self.compactFormat}',
122+
logging.INFO: f'[>] {self.compactFormat}',
123+
logging.WARNING: f'[!] {self.compactFormat}',
124+
logging.ERROR: f'[x] {self.compactFormat}',
121125
},
122126
timeZone=self.timeZone
123127
)
124128

125129
def _get_full_formatter(self):
126130
""" Returns full `Formatter` after initiating it for all levels """
127-
s1 = " " * 10
128-
s2 = " " * 50
129131
return LevelFormatter(
130132
{
131-
logging.DEBUG: f'~ [%(asctime)s] [%(levelname)s: %(module)s-%(lineno)d] {s1} > %(message)s {s2} [%(threadName)s]',
132-
logging.INFO: f'> [%(asctime)s] [%(levelname)s: %(module)s-%(lineno)d] {s1} > %(message)s {s2} [%(threadName)s]',
133-
logging.ERROR: f'[x] [%(asctime)s] [%(levelname)s: %(module)s-%(lineno)d] {s1} [x] %(message)s {s2} [%(threadName)s]',
133+
logging.DEBUG: f'[~] {self.fullFormat}',
134+
logging.INFO: f'[>] {self.fullFormat}',
135+
logging.WARNING: f'[!] {self.fullFormat}',
136+
logging.ERROR: f'[x] {self.fullFormat}',
134137
},
135138
datefmt=f"%Y-%m-%d %I:%M:%S %p ({self.timeZone})",
136139
timeZone=self.timeZone

0 commit comments

Comments
 (0)