Skip to content

Commit dd6bebd

Browse files
authored
Merge pull request #555 from hubmapconsortium/yuanzhou/log
Yuanzhou/log
2 parents 63cbe43 + 457c744 commit dd6bebd

File tree

5 files changed

+21
-28
lines changed

5 files changed

+21
-28
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ venv-entity-api
1010
#Mac DS Store files
1111
.DS_Store
1212

13+
# Ignore docker env file
14+
docker/.env
15+
1316
# Ignore copied files
1417
docker/entity-api/src
1518
docker/entity-api/VERSION

docker/docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ services:
3434
- "./entity-api/nginx/conf.d:/etc/nginx/conf.d"
3535
networks:
3636
- gateway_hubmap
37+
# Send docker logs to AWS CloudWatch
38+
logging:
39+
driver: awslogs
40+
options:
41+
awslogs-region: us-east-1
42+
awslogs-group: entity-api-docker-logs
43+
awslogs-stream: ${LOG_STREAM}
3744

3845
networks:
3946
# This is the network created by gateway to enable communicaton between multiple docker-compose projects

docker/example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LOG_STREAM=DEV

src/app.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,15 @@
4343
# Root logger configuration
4444
global logger
4545

46+
# Set logging format and level (default is warning)
47+
# All the API logging is forwarded to the uWSGI server and gets written into the log file `log/uwsgi-entity-api.log`
48+
# Log rotation is handled via logrotate on the host system with a configuration file
49+
# Do NOT handle log file and rotation via the Python logging to avoid issues with multi-worker processes
50+
logging.basicConfig(format='[%(asctime)s] %(levelname)s in %(module)s: %(message)s', level=logging.DEBUG, datefmt='%Y-%m-%d %H:%M:%S')
51+
4652
# Use `getLogger()` instead of `getLogger(__name__)` to apply the config to the root logger
4753
# will be inherited by the sub-module loggers
48-
try:
49-
logger = logging.getLogger()
50-
logger.setLevel(logging.DEBUG) # logger.setLevel(logging.INFO)
51-
52-
# All the API logging is gets written into the same log file
53-
# The uWSGI logging for each deployment disables the request logging
54-
# but still captures the 4xx and 5xx errors to the file `log/uwsgi-entity-api.log`
55-
# Log rotation is handled via logrotate on the host system with a configuration file
56-
# Do NOT handle log file and rotation via the Python logging to avoid issues with multi-worker processes
57-
log_file_handler = logging.FileHandler('../log/entity-api-' + time.strftime("%m-%d-%Y-%H-%M-%S") + '.log')
58-
log_file_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s'))
59-
logger.addHandler(log_file_handler)
60-
except Exception as e:
61-
print("Error setting up global log file.")
62-
print(str(e))
63-
64-
try:
65-
logger.info("logger initialized")
66-
except Exception as e:
67-
print("Error opening log file during startup")
68-
print(str(e))
54+
logger = logging.getLogger()
6955

7056
# Specify the absolute path of the instance folder and use the config file relative to the instance path
7157
app = Flask(__name__, instance_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'instance'), instance_relative_config = True)

src/uwsgi.ini

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ chdir = /usr/src/app/src
55
# Application's callbale
66
module = wsgi:application
77

8-
# Disable request logging due to it doesn't work with the Python logging level
9-
# This also improves the performance
10-
# We still want to log the 4xx and 5xx errors to the file
11-
disable-logging = true
12-
log-4xx = true
13-
log-5xx = true
14-
logto = /usr/src/app/log/uwsgi-entity-api.log
8+
# Delegate the logging to the master process
9+
# Send logs to stdout instead of file so docker picks it up and writes to AWS CloudWatch
10+
log-master=true
1511

1612
# Master with 16 worker processes (based on CPU number)
1713
master = true

0 commit comments

Comments
 (0)