Skip to content

Commit bc046e6

Browse files
committed
first commit
0 parents  commit bc046e6

File tree

8 files changed

+3886
-0
lines changed

8 files changed

+3886
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
logfile.log

README.md

Whitespace-only changes.

docker-compose.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '3.7'
2+
services:
3+
elasticsearch:
4+
image: 'docker.elastic.co/elasticsearch/elasticsearch:8.10.2'
5+
container_name: elasticsearch-logging
6+
volumes:
7+
- 'data_es:/usr/share/elasticsearch/data'
8+
environment:
9+
- discovery.type=single-node
10+
- CLI_JAVA_OPTS=-Xms2g -Xmx2g
11+
- bootstrap.memory_lock=true
12+
- xpack.security.enabled=false
13+
- xpack.security.enrollment.enabled=false
14+
ports:
15+
- '9300:9200'
16+
networks:
17+
- elasticsearch-kibana-node-js-logging
18+
kibana:
19+
image: 'docker.elastic.co/kibana/kibana:8.10.2'
20+
container_name: kibina-logging
21+
environment:
22+
- 'ELASTICSEARCH_HOSTS=http://elasticsearch:9200'
23+
- 'XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY=d1a66dfd-c4d3-4a0a-8290-2abcb83ab3aa'
24+
ports:
25+
- '5701:5601'
26+
networks:
27+
- elasticsearch-kibana-node-js-logging
28+
app:
29+
build: .
30+
container_name: app-logging
31+
restart: unless-stopped
32+
ports:
33+
- '3002:8000'
34+
stdin_open: true
35+
tty: true
36+
networks:
37+
- elasticsearch-kibana-node-js-logging
38+
volumes:
39+
data_es:
40+
driver: local
41+
networks:
42+
elasticsearch-kibana-node-js-logging: null

dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:20
2+
3+
WORKDIR /app
4+
COPY package.json .
5+
RUN npm install
6+
COPY . .
7+
EXPOSE 8000
8+
CMD npm start

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var express = require('express');
2+
var app = express();
3+
const logger = require('./logger');
4+
5+
app.get('/', function (req, res) {
6+
res.send('Hello World.');
7+
logger.info('I am infor message'); //logs the info log message
8+
logger.error('I am error message'); //logs the error log message
9+
});
10+
11+
app.listen(8000, function () {
12+
console.log('Listening on port 8000!');
13+
});

logger.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const winston = require('winston');
2+
const { ElasticsearchTransport } = require('winston-elasticsearch');
3+
4+
const esTransportOpts = {
5+
level: 'info',
6+
index: 'an-index',
7+
clientOpts: {
8+
node: 'http://elasticsearch:9200', // Replace with your Elasticsearch node URL or you can place it to the .env file
9+
10+
},
11+
transformer: logData => {
12+
return {
13+
"@timestamp": (new Date()).getTime(),
14+
severity: logData.level,
15+
message: `[${logData.level}] LOG Message: ${logData.message}`,
16+
fields: {}
17+
}
18+
}
19+
};
20+
21+
const logger = winston.createLogger({
22+
level: 'info',
23+
format: winston.format.json(),
24+
transports: [
25+
new winston.transports.File({ filename: 'logfile.log', level: 'error' }), // Save errors to file
26+
new ElasticsearchTransport(esTransportOpts) // Use ElasticsearchTransport instead of Elasticsearch
27+
]
28+
});
29+
30+
if (process.env.NODE_ENV !== 'production') {
31+
logger.add(new winston.transports.Console({ // Log to console if not in production
32+
format: winston.format.simple()
33+
}));
34+
}
35+
36+
module.exports = logger;

0 commit comments

Comments
 (0)