Skip to content

Commit 02b56d8

Browse files
authored
Setup logging with Python native logging library. (#28)
1 parent c150286 commit 02b56d8

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

application/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021-2022 iCtrl Developers
1+
# Copyright (c) 2021-2024 iCtrl Developers
22
#
33
# Permission is hereby granted, free of charge, to any person obtaining a copy
44
# of this software and associated documentation files (the "Software"), to
@@ -17,13 +17,24 @@
1717
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1818
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
# IN THE SOFTWARE.
20-
20+
import logging.config
2121
import os
2222
import sys
2323

24+
import yaml
2425
from flask import Flask, Blueprint
2526
from werkzeug.serving import WSGIRequestHandler
2627

28+
try:
29+
with open('log_config.yaml', 'r') as config_file:
30+
config = yaml.safe_load(config_file.read())
31+
logging.config.dictConfig(config)
32+
except Exception as ex:
33+
print("Logging setup failed with exception = ", ex)
34+
35+
logger = logging.getLogger(__name__)
36+
logger.warning(f"Logging is set up with config={config}")
37+
2738
from .Profile.Profile import Profile
2839

2940
# enable persistent HTTP connections (keep-alive)
@@ -54,9 +65,11 @@
5465
profiles: Profile
5566
if os.getenv('DBADDR') is not None:
5667
from .Profile.DBProfile import DBProfile
68+
5769
profiles = DBProfile(app)
5870
else:
5971
from .Profile.LocalProfile import LocalProfile
72+
6073
profiles = LocalProfile()
6174

6275
api = Blueprint('api', __name__)

log_config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: 1
2+
disable_existing_loggers: True
3+
4+
formatters:
5+
default:
6+
format: '%(asctime)s %(levelname)s [%(name)s:%(lineno)d] %(message)s'
7+
datefmt: '%Y-%m-%d %H:%M:%S'
8+
9+
handlers:
10+
console:
11+
class: logging.StreamHandler
12+
level: DEBUG
13+
formatter: default
14+
stream: ext://sys.stderr
15+
16+
root:
17+
level: DEBUG
18+
handlers: [console]

publish/ictrl_be.spec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ block_cipher = None
88
a = Analysis(['../ictrl_be.py'],
99
pathex=['.'],
1010
binaries=[],
11-
datas=[('../client/build', './client')],
11+
datas=[('../client/build', './client'),
12+
('../log_config.yaml', '.')],
1213
hiddenimports=[],
1314
hookspath=[],
1415
hooksconfig={},

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Werkzeug==2.2.2
1212
zipstream-new==1.1.8
1313
paramiko==3.0.0
1414
numpy==1.24.2
15+
pyyaml==6.0.1
1516
git+https://github.com/junhaoliao/simple-websocket-server#egg=SimpleWebSocketServer

0 commit comments

Comments
 (0)