Skip to content

Commit ecefcdc

Browse files
Add files via upload
1 parent 2c21f8b commit ecefcdc

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

log/how to log messages.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
filename='HISTORYlistener.log',
5+
level=logging.DEBUG,
6+
format='%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s',
7+
datefmt='%Y-%m-%d %H:%M:%S',
8+
)
9+
10+
####################################################################################################
11+
12+
import logging
13+
14+
class MyFormatter(logging.Formatter):
15+
def format(self, record):
16+
record.message2 = ""
17+
if(record.args):
18+
record.func_name = record.args.get("func_name", "Fallback Value")
19+
return super().format(record)
20+
21+
logger = logging.getLogger()
22+
fh = logging.FileHandler(filename='log.txt', mode='a')
23+
fh.setFormatter(MyFormatter('%(asctime)s --- %(func_name)s --- %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
24+
logging.basicConfig(level=logging.INFO, handlers=[fh])
25+
26+
def write_log(func_name, message):
27+
logger.info(message, {"func_name": func_name})
28+

log/logging_tst.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import logging
2+
logging.basicConfig(filename='example.log',level=logging.DEBUG)
3+
logging.debug('This message should go to the log file')
4+
logging.info('So should this')
5+
logging.warning('And this, too')

0 commit comments

Comments
 (0)