Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit e661ee1

Browse files
authored
Add logging to ELK stack (#57)
* Add ELK stack * Cleanup * Fix tests * Add logstash settings * Fix logging settings * Save logs in ELK
1 parent feb7708 commit e661ee1

File tree

5 files changed

+110
-68
lines changed

5 files changed

+110
-68
lines changed

client/karma.conf.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ module.exports = function (config) {
1515
client:{
1616
clearContext: false // leave Jasmine Spec Runner output visible in browser
1717
},
18-
files: [
19-
{ pattern: './src/test.ts', watched: false }
20-
],
18+
files: [{
19+
pattern: './node_modules/@angular/material/prebuilt-themes/indigo-pink.css',
20+
included: true,
21+
watched: true
22+
}, {
23+
pattern: './src/test.ts',
24+
watched: false
25+
}],
2126
preprocessors: {
2227
'./src/test.ts': ['@angular/cli']
2328
},

docker-compose.yml

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -73,71 +73,71 @@ services:
7373
networks:
7474
- example_cluster
7575

76-
# visualizer:
77-
# image: dockersamples/visualizer:stable
78-
# ports:
79-
# - "8080:8080"
80-
# volumes:
81-
# - "/var/run/docker.sock:/var/run/docker.sock"
82-
# deploy:
83-
# placement:
84-
# constraints: [node.role == manager]
85-
# networks:
86-
# - example_cluster
87-
#
88-
# elasticsearch:
89-
# image: elasticsearch:5.4.3
90-
# command: elasticsearch
91-
# environment:
92-
# ES_JAVA_OPTS: -Xms1g -Xmx1g
93-
# ports:
94-
# - "9200:9200"
95-
# - "9300:9300"
96-
# ulimits:
97-
# memlock: -1
98-
# nofile:
99-
# hard: 65536
100-
# soft: 65536
101-
# nproc: 65538
102-
# volumes:
103-
# - esdata:/usr/share/elasticsearch/data:rw
104-
# networks:
105-
# - example_cluster
106-
# deploy:
107-
# mode: replicated
108-
# replicas: 1
109-
# healthcheck:
110-
# test: curl -s http://localhost:9200/_cluster/health | egrep 'yellow|green'
111-
# interval: 30s
112-
# retries: 1
113-
#
114-
# kibana:
115-
# image: kibana:5.4.3
116-
# ports:
117-
# - "5601:5601"
118-
# environment:
119-
# ELASTICSEARCH_URL: http://elasticsearch:9200
120-
# networks:
121-
# - example_cluster
122-
# deploy:
123-
# mode: replicated
124-
# replicas: 1
125-
# healthcheck:
126-
# test: wget -qO- http://localhost:5601 > /dev/null
127-
# interval: 30s
128-
# retries: 1
129-
#
130-
# logstash:
131-
# image: logstash:5.4.3
132-
# command: sh -c "logstash -e 'input { tcp { port => 5959 codec => json } } output { stdout { codec => rubydebug } elasticsearch { hosts => [ \"elasticsearch\" ] } }'"
133-
# ports:
134-
# - "5959:5959/tcp"
135-
# - "12201:12201/udp"
136-
# networks:
137-
# - example_cluster
138-
# deploy:
139-
# mode: replicated
140-
# replicas: 1
76+
visualizer:
77+
image: dockersamples/visualizer:stable
78+
ports:
79+
- "8080:8080"
80+
volumes:
81+
- "/var/run/docker.sock:/var/run/docker.sock"
82+
deploy:
83+
placement:
84+
constraints: [node.role == manager]
85+
networks:
86+
- example_cluster
87+
88+
elasticsearch:
89+
image: elasticsearch:5.4.3
90+
command: elasticsearch
91+
environment:
92+
ES_JAVA_OPTS: -Xms1g -Xmx1g
93+
ports:
94+
- "9200:9200"
95+
- "9300:9300"
96+
ulimits:
97+
memlock: -1
98+
nofile:
99+
hard: 65536
100+
soft: 65536
101+
nproc: 65538
102+
volumes:
103+
- esdata:/usr/share/elasticsearch/data:rw
104+
networks:
105+
- example_cluster
106+
deploy:
107+
mode: replicated
108+
replicas: 1
109+
healthcheck:
110+
test: curl -s http://localhost:9200/_cluster/health | egrep 'yellow|green'
111+
interval: 30s
112+
retries: 1
113+
114+
kibana:
115+
image: kibana:5.4.3
116+
ports:
117+
- "5601:5601"
118+
environment:
119+
ELASTICSEARCH_URL: http://elasticsearch:9200
120+
networks:
121+
- example_cluster
122+
deploy:
123+
mode: replicated
124+
replicas: 1
125+
healthcheck:
126+
test: wget -qO- http://localhost:5601 > /dev/null
127+
interval: 30s
128+
retries: 1
129+
130+
logstash:
131+
image: logstash:5.4.3
132+
command: sh -c "logstash -e 'input { tcp { port => 5959 codec => json } } output { stdout { codec => rubydebug } elasticsearch { hosts => [ \"elasticsearch\" ] } }'"
133+
ports:
134+
- "5959:5959/tcp"
135+
- "12201:12201/udp"
136+
networks:
137+
- example_cluster
138+
deploy:
139+
mode: replicated
140+
replicas: 1
141141

142142
volumes:
143143
esdata:

server/api/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import logging
2+
import logstash
3+
14
from rest_framework import viewsets
25
from django.contrib.auth.models import User
36
from api.serializers import UserSerializer
47

8+
logstash_logger = logging.getLogger('python-logstash-logger')
9+
logstash_logger.setLevel(logging.INFO)
10+
logstash_logger.addHandler(logstash.TCPLogstashHandler('logstash', 5959, version=1))
11+
512

613
class UserViewSet(viewsets.ReadOnlyModelViewSet):
714
"""
815
API endpoint that allows users to be viewed or edited.
916
"""
17+
logstash_logger.info('logstash-server: test logstash info message.')
1018
queryset = User.objects.all().order_by('-date_joined')
1119
serializer_class = UserSerializer

server/config/settings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,32 @@
112112
},
113113
]
114114

115+
# Logging
116+
# https://github.com/vklochan/python-logstash
117+
LOGGING = {
118+
'version': 1,
119+
'disable_existing_loggers': True,
120+
'handlers': {
121+
'logstash': {
122+
'level': 'DEBUG',
123+
'class': 'logstash.LogstashHandler',
124+
'host': 'localhost',
125+
'port': 5959,
126+
'version': 1,
127+
'message_type': 'logstash',
128+
'fqdn': False,
129+
'tags': ['tag1', 'tag2'],
130+
},
131+
},
132+
'loggers': {
133+
'django.request': {
134+
'handlers': ['logstash'],
135+
'level': 'DEBUG',
136+
'propagate': True,
137+
},
138+
},
139+
}
140+
115141

116142
# Internationalization
117143
# https://docs.djangoproject.com/en/1.11/topics/i18n/

server/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ django-filter==1.0.2
1515

1616
# CORS
1717
django-cors-headers==2.0.2
18+
19+
# Logging
20+
python-logstash==0.4.6

0 commit comments

Comments
 (0)