Skip to content

Commit 2f236cd

Browse files
committed
Configure logging settings with environment variable and .ini config file.
1 parent 9249ec1 commit 2f236cd

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

config.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[Logging]
2+
log_level = INFO
3+
format = [%%(asctime)s] %%(levelname)s in %%(module)s: %%(message)s

flaskapp/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LOG_LEVEL=DEBUG
2+
#LOG_LEVEL=WARN

flaskapp/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
from flask import Flask
22
from logging.config import dictConfig
33

4+
import configparser
5+
from dotenv import load_dotenv
6+
import os
7+
48

59
def create_app():
610
"""
711
The application factory.
812
"""
913

14+
load_dotenv()
15+
16+
config = configparser.ConfigParser()
17+
config.read('config.ini')
18+
19+
log_level = os.environ.get('LOG_LEVEL', config['Logging'].get('log_level'))
20+
log_format = config['Logging'].get('format')
21+
1022
dictConfig({
1123
'version': 1,
1224
'formatters': {'default': {
13-
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
25+
'format': log_format,
1426
}},
1527
'handlers': {'wsgi': {
1628
'class': 'logging.StreamHandler',
1729
'stream': 'ext://flask.logging.wsgi_errors_stream',
1830
'formatter': 'default'
1931
}},
2032
'root': {
21-
'level': 'INFO',
33+
'level': log_level,
2234
'handlers': ['wsgi']
2335
}
2436
})
2537

2638
app = Flask(__name__)
39+
app.logger.debug(f'Configurations:-\nLog level: {log_level}\nLog format: {log_format}')
2740
return app

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ MarkupSafe==2.1.3
99
psycopg2-binary==2.9.6
1010
Werkzeug==2.3.6
1111
swagger-ui-py==23.9.23
12-
gunicorn==21.0.1
12+
gunicorn==21.0.1
13+
python_dotenv==1.0.1

0 commit comments

Comments
 (0)