Skip to content

Commit

Permalink
Setup logging with Python native logging library. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-ruihao authored Jul 25, 2024
1 parent c150286 commit 02b56d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
17 changes: 15 additions & 2 deletions application/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021-2022 iCtrl Developers
# Copyright (c) 2021-2024 iCtrl Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
Expand All @@ -17,13 +17,24 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import logging.config
import os
import sys

import yaml
from flask import Flask, Blueprint
from werkzeug.serving import WSGIRequestHandler

try:
with open('log_config.yaml', 'r') as config_file:
config = yaml.safe_load(config_file.read())
logging.config.dictConfig(config)
except Exception as ex:
print("Logging setup failed with exception = ", ex)

logger = logging.getLogger(__name__)
logger.warning(f"Logging is set up with config={config}")

from .Profile.Profile import Profile

# enable persistent HTTP connections (keep-alive)
Expand Down Expand Up @@ -54,9 +65,11 @@
profiles: Profile
if os.getenv('DBADDR') is not None:
from .Profile.DBProfile import DBProfile

profiles = DBProfile(app)
else:
from .Profile.LocalProfile import LocalProfile

profiles = LocalProfile()

api = Blueprint('api', __name__)
Expand Down
18 changes: 18 additions & 0 deletions log_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 1
disable_existing_loggers: True

formatters:
default:
format: '%(asctime)s %(levelname)s [%(name)s:%(lineno)d] %(message)s'
datefmt: '%Y-%m-%d %H:%M:%S'

handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: default
stream: ext://sys.stderr

root:
level: DEBUG
handlers: [console]
3 changes: 2 additions & 1 deletion publish/ictrl_be.spec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ block_cipher = None
a = Analysis(['../ictrl_be.py'],
pathex=['.'],
binaries=[],
datas=[('../client/build', './client')],
datas=[('../client/build', './client'),
('../log_config.yaml', '.')],
hiddenimports=[],
hookspath=[],
hooksconfig={},
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Werkzeug==2.2.2
zipstream-new==1.1.8
paramiko==3.0.0
numpy==1.24.2
pyyaml==6.0.1
git+https://github.com/junhaoliao/simple-websocket-server#egg=SimpleWebSocketServer

0 comments on commit 02b56d8

Please sign in to comment.