-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.py
More file actions
31 lines (25 loc) · 816 Bytes
/
Copy pathlogs.py
File metadata and controls
31 lines (25 loc) · 816 Bytes
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
import logging
import os
class FsyncFileHandler(logging.FileHandler):
"""FileHandler that fsyncs to disk after every emit so logs survive abrupt termination."""
def emit(self, record):
super().emit(record)
if self.stream is not None:
try:
self.stream.flush()
os.fsync(self.stream.fileno())
except (OSError, ValueError):
pass
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if not logger.handlers:
_fmt = logging.Formatter(
"%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
_fh = FsyncFileHandler("app.log")
_fh.setFormatter(_fmt)
logger.addHandler(_fh)
_sh = logging.StreamHandler()
_sh.setFormatter(_fmt)
logger.addHandler(_sh)