Skip to content

Commit 08c579c

Browse files
authored
Merge pull request #138 from NodeFactoryIo/feature/log-formatting
Feature/log formatting
2 parents 14d36fc + ad65753 commit 08c579c

File tree

6 files changed

+175
-20
lines changed

6 files changed

+175
-20
lines changed

.env.sample

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ UPTIME_NOTIF_RECURRENCE=0 * * * *
4343
SWAGGER_DOCS_PORT=8080
4444
# Swagger docs default url
4545
SWAGGER_DOCS_URL=/api/swagger
46+
47+
# Winston Elasticsearch settings
48+
49+
# Elasticsearch default url
50+
ELASTIC_LOG_URL=http://localhost
51+
# Elasticsearch default port
52+
ELASTIC_LOG_PORT=9200
53+
# Elasticsearch default log level
54+
ELASTIC_LOG_LEVEL=info
55+
# Elasticsearch logging
56+
ELASTIC_LOGGING=disabled

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"sequelize-typescript": "^1.1.0",
5353
"swagger-ui-express": "^4.1.3",
5454
"umzug": "^2.2.0",
55-
"winston": "^3.2.1"
55+
"winston": "^3.2.1",
56+
"winston-elasticsearch": "^0.8.7"
5657
},
5758
"devDependencies": {
5859
"@types/bcryptjs": "^2.4.2",

src/App.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export class App implements Service {
2525
constructor() {
2626
this.express = express();
2727
// add before route middleware's here
28-
this.express.use(morgan("short", {stream: morganLogger}) as RequestHandler);
28+
if (config.env == "dev") {
29+
this.express.use(morgan("short", {stream: morganLogger}) as RequestHandler);
30+
}
2931
this.express.use(bodyParser.json());
3032
this.express.use(helmet() as RequestHandler);
3133
// add after route middleware's here

src/Config/Config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ const config = Object.freeze({
2828
retryCount: process.env.EMAIL_RETRY_COUNT || 3
2929
},
3030
// cron like definition (every hour by default)
31-
uptimeNotificationsRecurrenceRule: process.env.UPTIME_NOTIF_RECURRENCE || "0 * * * *"
31+
uptimeNotificationsRecurrenceRule: process.env.UPTIME_NOTIF_RECURRENCE || "0 * * * *",
32+
elasticsearch: {
33+
url: process.env.ELASTIC_LOG_URL || "http://localhost",
34+
port: process.env.ELASTIC_LOG_PORT || 9200,
35+
level: process.env.ELASTIC_LOG_LEVEL || "info"
36+
},
37+
logging: {
38+
es: process.env.ELASTIC_LOGGING || "disabled"
39+
}
3240
});
3341

3442
export default config;

src/Services/Logger.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
import winston from "winston";
2+
import ElasticsearchTransport, {LogData} from "winston-elasticsearch"
3+
import {ClientOptions} from "@elastic/elasticsearch"
4+
5+
import config from "../Config/Config";
26

37
const logger = winston.createLogger({
4-
format: winston.format.combine(
5-
winston.format.colorize(),
6-
winston.format.timestamp(),
7-
winston.format.json(),
8-
),
98
level: "info",
9+
transports: new winston.transports.Console({
10+
format: winston.format.combine(
11+
winston.format.timestamp({
12+
format: 'YYYY-MM-DD HH:mm:ss'
13+
}),
14+
winston.format.printf(info => `${info.timestamp} | ${info.level} | ${info.message}`),
15+
),
16+
}),
1017
});
1118

12-
logger.add(new winston.transports.Console({
13-
format: winston.format.simple(),
14-
}));
19+
const esTransportOpts = {
20+
level: 'info',
21+
transformer: (logData: LogData) => {
22+
return {
23+
"@timestamp": (new Date()).toLocaleString(),
24+
severity: logData.level,
25+
message: logData.message
26+
}
27+
},
28+
clientOpts: {
29+
node: `${config.elasticsearch.url}:${config.elasticsearch.port}`,
30+
log: `${config.elasticsearch.level}`
31+
} as ClientOptions,
32+
};
33+
34+
if (config.env == "production" && config.logging.es === "enabled") {
35+
logger.add(new (ElasticsearchTransport as any)(esTransportOpts));
36+
}
1537

1638
export default logger;
1739

yarn.lock

Lines changed: 120 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@
9898
lodash "^4.17.5"
9999
to-fast-properties "^2.0.0"
100100

101+
"@elastic/elasticsearch@^7.6.1":
102+
version "7.6.1"
103+
resolved "https://registry.yarnpkg.com/@elastic/elasticsearch/-/elasticsearch-7.6.1.tgz#f23fb6146deb8053c1acc19bc4a6ddf6f9961a1e"
104+
integrity sha512-TEffCqUM9mK91eScc8pC/7p5+hF3+dXuz1lm40sxsB6495ilcVz7nn31BtOoocRTtmGdHjsCQRZqPpM0caatcQ==
105+
dependencies:
106+
debug "^4.1.1"
107+
decompress-response "^4.2.0"
108+
into-stream "^5.1.0"
109+
ms "^2.1.1"
110+
once "^1.4.0"
111+
pump "^3.0.0"
112+
secure-json-parse "^2.1.0"
113+
101114
102115
version "2.0.0"
103116
resolved "https://registry.yarnpkg.com/@hapi/address/-/address-2.0.0.tgz#9f05469c88cb2fd3dcd624776b54ee95c312126a"
@@ -621,6 +634,11 @@ arrify@^1.0.1:
621634
version "1.0.1"
622635
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
623636

637+
asap@~2.0.6:
638+
version "2.0.6"
639+
resolved "https://registry.yarnpkg.com/asap/-/asap-2.0.6.tgz#e50347611d7e690943208bbdafebcbc2fb866d46"
640+
integrity sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=
641+
624642
asn1@~0.2.3:
625643
version "0.2.4"
626644
resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.4.tgz#8d2475dfab553bb33e77b54e59e880bb8ce23136"
@@ -1255,6 +1273,11 @@ [email protected]:
12551273
version "2.0.0"
12561274
resolved "https://registry.yarnpkg.com/dasherize/-/dasherize-2.0.0.tgz#6d809c9cd0cf7bb8952d80fc84fa13d47ddb1308"
12571275

1276+
dayjs@^1.8.23:
1277+
version "1.8.23"
1278+
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.8.23.tgz#07b5a8e759c4d75ae07bdd0ad6977f851c01e510"
1279+
integrity sha512-NmYHMFONftoZbeOhVz6jfiXI4zSiPN6NoVWJgC0aZQfYVwzy/ZpESPHuCcI0B8BUMpSJQ08zenHDbofOLKq8hQ==
1280+
12581281
debug-log@^1.0.1:
12591282
version "1.0.1"
12601283
resolved "https://registry.yarnpkg.com/debug-log/-/debug-log-1.0.1.tgz#2307632d4c04382b8df8a32f70b895046d52745f"
@@ -1272,20 +1295,20 @@ [email protected], debug@^3.1.0, debug@^3.2.6:
12721295
dependencies:
12731296
ms "^2.1.1"
12741297

1298+
[email protected], debug@^4.0.1, debug@^4.1.1:
1299+
version "4.1.1"
1300+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
1301+
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
1302+
dependencies:
1303+
ms "^2.1.1"
1304+
12751305
debug@=3.1.0:
12761306
version "3.1.0"
12771307
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
12781308
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
12791309
dependencies:
12801310
ms "2.0.0"
12811311

1282-
debug@^4.0.1, debug@^4.1.1:
1283-
version "4.1.1"
1284-
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
1285-
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
1286-
dependencies:
1287-
ms "^2.1.1"
1288-
12891312
decamelize@^1.1.1, decamelize@^1.2.0:
12901313
version "1.2.0"
12911314
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
@@ -1294,6 +1317,13 @@ decode-uri-component@^0.2.0:
12941317
version "0.2.0"
12951318
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
12961319

1320+
decompress-response@^4.2.0:
1321+
version "4.2.1"
1322+
resolved "https://registry.yarnpkg.com/decompress-response/-/decompress-response-4.2.1.tgz#414023cc7a302da25ce2ec82d0d5238ccafd8986"
1323+
integrity sha512-jOSne2qbyE+/r8G1VU+G/82LBs2Fs4LAsTiLSHOCOMZQl2OKZ6i8i4IyHemTe+/yIXOtTcRQMzPcgyhoFlqPkw==
1324+
dependencies:
1325+
mimic-response "^2.0.0"
1326+
12971327
deep-eql@^3.0.1:
12981328
version "3.0.1"
12991329
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
@@ -1987,6 +2017,14 @@ [email protected]:
19872017
version "0.5.2"
19882018
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
19892019

2020+
from2@^2.3.0:
2021+
version "2.3.0"
2022+
resolved "https://registry.yarnpkg.com/from2/-/from2-2.3.0.tgz#8bfb5502bde4a4d36cfdeea007fcca21d7e382af"
2023+
integrity sha1-i/tVAr3kpNNs/e6gB/zKIdfjgq8=
2024+
dependencies:
2025+
inherits "^2.0.1"
2026+
readable-stream "^2.0.0"
2027+
19902028
fs-extra@^7.0.1:
19912029
version "7.0.1"
19922030
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
@@ -2369,7 +2407,7 @@ inherits@2, [email protected], inherits@~2.0.1, inherits@~2.0.3:
23692407
version "2.0.3"
23702408
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
23712409

2372-
[email protected], inherits@^2.0.3:
2410+
[email protected], inherits@^2.0.1, inherits@^2.0.3:
23732411
version "2.0.4"
23742412
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
23752413
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
@@ -2397,6 +2435,14 @@ inquirer@^7.0.0:
23972435
strip-ansi "^5.1.0"
23982436
through "^2.3.6"
23992437

2438+
into-stream@^5.1.0:
2439+
version "5.1.1"
2440+
resolved "https://registry.yarnpkg.com/into-stream/-/into-stream-5.1.1.tgz#f9a20a348a11f3c13face22763f2d02e127f4db8"
2441+
integrity sha512-krrAJ7McQxGGmvaYbB7Q1mcA+cRwg9Ij2RfWIeVesNBgVDZmzY/Fa4IpZUT3bmdRzMzdf/mzltCG2Dq99IZGBA==
2442+
dependencies:
2443+
from2 "^2.3.0"
2444+
p-is-promise "^3.0.0"
2445+
24002446
invariant@^2.2.0:
24012447
version "2.2.4"
24022448
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
@@ -2927,6 +2973,11 @@ locate-path@^3.0.0:
29272973
p-locate "^3.0.0"
29282974
path-exists "^3.0.0"
29292975

2976+
lodash.defaults@^4.2.0:
2977+
version "4.2.0"
2978+
resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"
2979+
integrity sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=
2980+
29302981
lodash.includes@^4.3.0:
29312982
version "4.3.0"
29322983
resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
@@ -2957,6 +3008,11 @@ lodash.isstring@^4.0.1:
29573008
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
29583009
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
29593010

3011+
lodash.omit@^4.5.0:
3012+
version "4.5.0"
3013+
resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60"
3014+
integrity sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA=
3015+
29603016
lodash.once@^4.0.0:
29613017
version "4.1.1"
29623018
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
@@ -3203,6 +3259,11 @@ mimic-fn@^2.0.0, mimic-fn@^2.1.0:
32033259
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
32043260
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==
32053261

3262+
mimic-response@^2.0.0:
3263+
version "2.1.0"
3264+
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-2.1.0.tgz#d13763d35f613d09ec37ebb30bac0469c0ee8f43"
3265+
integrity sha512-wXqjST+SLt7R009ySCglWBCFpjUygmCIfD790/kVbiGmUgfYGuB14PiTd5DwVxSV4NcYHjzMkoj5LjQZwTQLEA==
3266+
32063267
[email protected], minimatch@^3.0.4:
32073268
version "3.0.4"
32083269
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
@@ -3633,6 +3694,11 @@ p-is-promise@^2.0.0:
36333694
resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-2.1.0.tgz#918cebaea248a62cf7ffab8e3bca8c5f882fc42e"
36343695
integrity sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==
36353696

3697+
p-is-promise@^3.0.0:
3698+
version "3.0.0"
3699+
resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-3.0.0.tgz#58e78c7dfe2e163cf2a04ff869e7c1dba64a5971"
3700+
integrity sha512-Wo8VsW4IRQSKVXsJCn7TomUaVtyfjVDn3nUP7kE967BQk0CwFpdbZs0X0uk5sW9mkBa9eNM7hCMaG93WUAwxYQ==
3701+
36363702
p-limit@^1.1.0:
36373703
version "1.3.0"
36383704
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.3.0.tgz#b86bd5f0c25690911c7590fcbfc2010d54b3ccb8"
@@ -3909,6 +3975,13 @@ progress@^2.0.0:
39093975
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
39103976
integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==
39113977

3978+
promise@^8.1.0:
3979+
version "8.1.0"
3980+
resolved "https://registry.yarnpkg.com/promise/-/promise-8.1.0.tgz#697c25c3dfe7435dd79fcd58c38a135888eaf05e"
3981+
integrity sha512-W04AqnILOL/sPRXziNicCjSNRruLAuIHEOVBazepu0545DDNGYHz7ar9ZgZ1fMU8/MA4mVxp5rkBWRi6OXIy3Q==
3982+
dependencies:
3983+
asap "~2.0.6"
3984+
39123985
proto-list@~1.2.1:
39133986
version "1.2.4"
39143987
resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
@@ -4026,6 +4099,19 @@ [email protected]:
40264099
string_decoder "~1.0.0"
40274100
util-deprecate "~1.0.1"
40284101

4102+
readable-stream@^2.0.0:
4103+
version "2.3.7"
4104+
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57"
4105+
integrity sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==
4106+
dependencies:
4107+
core-util-is "~1.0.0"
4108+
inherits "~2.0.3"
4109+
isarray "~1.0.0"
4110+
process-nextick-args "~2.0.0"
4111+
safe-buffer "~5.1.1"
4112+
string_decoder "~1.1.1"
4113+
util-deprecate "~1.0.1"
4114+
40294115
readable-stream@^2.0.5, readable-stream@^2.1.5, readable-stream@^2.3.6:
40304116
version "2.3.6"
40314117
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf"
@@ -4183,6 +4269,11 @@ retry-as-promised@^3.2.0:
41834269
dependencies:
41844270
any-promise "^1.3.0"
41854271

4272+
retry@^0.12.0:
4273+
version "0.12.0"
4274+
resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b"
4275+
integrity sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=
4276+
41864277
41874278
version "2.6.3"
41884279
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
@@ -4243,6 +4334,11 @@ safe-regex@^1.1.0:
42434334
version "2.1.2"
42444335
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
42454336

4337+
secure-json-parse@^2.1.0:
4338+
version "2.1.0"
4339+
resolved "https://registry.yarnpkg.com/secure-json-parse/-/secure-json-parse-2.1.0.tgz#ae76f5624256b5c497af887090a5d9e156c9fb20"
4340+
integrity sha512-GckO+MS/wT4UogDyoI/H/S1L0MCcKS1XX/vp48wfmU7Nw4woBmb8mIpu4zPBQjKlRT88/bt9xdoV4111jPpNJA==
4341+
42464342
semver-diff@^2.0.0:
42474343
version "2.1.0"
42484344
resolved "https://registry.yarnpkg.com/semver-diff/-/semver-diff-2.1.0.tgz#4bbb8437c8d37e4b0cf1a68fd726ec6d645d6d36"
@@ -5154,7 +5250,22 @@ widest-line@^2.0.0:
51545250
dependencies:
51555251
string-width "^2.1.1"
51565252

5157-
winston-transport@^4.3.0:
5253+
winston-elasticsearch@^0.8.7:
5254+
version "0.8.7"
5255+
resolved "https://registry.yarnpkg.com/winston-elasticsearch/-/winston-elasticsearch-0.8.7.tgz#fcb53a21200fd84bcebb5a260fd1487a8ac5f1b8"
5256+
integrity sha512-43Yn5y2dRyz8bxldnoaBE/RT9nibUrnfME2njWv/vsqV2cCRF3lDcP/4uvZTjypD1BYsEQNBx/Qn0p86RCa4rg==
5257+
dependencies:
5258+
"@elastic/elasticsearch" "^7.6.1"
5259+
dayjs "^1.8.23"
5260+
debug "4.1.1"
5261+
lodash.defaults "^4.2.0"
5262+
lodash.omit "^4.5.0"
5263+
promise "^8.1.0"
5264+
retry "^0.12.0"
5265+
winston "^3.2.1"
5266+
winston-transport "4.3.0"
5267+
5268+
[email protected], winston-transport@^4.3.0:
51585269
version "4.3.0"
51595270
resolved "https://registry.yarnpkg.com/winston-transport/-/winston-transport-4.3.0.tgz#df68c0c202482c448d9b47313c07304c2d7c2c66"
51605271
integrity sha512-B2wPuwUi3vhzn/51Uukcao4dIduEiPOcOt9HJ3QeaXgkJ5Z7UwpBzxS4ZGNHtrxrUvTwemsQiSys0ihOf8Mp1A==

0 commit comments

Comments
 (0)