@sera4/essentia 1.0.13 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/health/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  "use strict"
2
2
 
3
3
  const pathHelper = require("path");
4
- const logger = require("../logger/s4-logger");
5
- const lc = require("../last_commit");
6
4
 
7
5
  class HealthCheckError extends Error {
8
6
  constructor(m) {
@@ -11,7 +9,7 @@ class HealthCheckError extends Error {
11
9
  }
12
10
 
13
11
  /* Health Checking router + controller for use in all of our modules
14
- * Example use: Place the following code in your routes file
12
+ * Example use: Place the following code in your routes file
15
13
  *
16
14
  * const hc = require("@sera4/essentia").healthCheck;
17
15
  * const router = require('express').Router();
@@ -22,7 +20,7 @@ class HealthCheckError extends Error {
22
20
  */
23
21
 
24
22
  class HealthCheck {
25
- constructor(appendData = {}) {
23
+ constructor(appendData = {}) {
26
24
  this.defaultResponse = {
27
25
  ...
28
26
  appendData,
@@ -30,25 +28,17 @@ class HealthCheck {
30
28
  errors: {}
31
29
  };
32
30
 
33
- this.defaultPathRoute = `health_check\(.*\)?`
34
- this.lastCallID = null;
35
- this.lastCommit = "unknown"
36
-
37
- // dev versions don't generate this by default
38
- try {
39
- this.lastCommit = lc.getLastCommitSync().commit;
40
- } catch(e) {
41
- }
42
-
31
+ this.defaultPathRoute = `health_check\(.*\)?`
32
+ this.lastCallId = 0; // default start point/integer
43
33
  this.healthChecks = [];
44
34
  }
45
35
 
46
- /* call this with an async function, must support wait capability
47
- *
36
+ /* call this with an async function, must support wait capability
37
+ *
48
38
  * const dbCheck = async () => {
49
39
  * return { name: "db", "result": "success", critical: false };
50
40
  * }
51
- */
41
+ */
52
42
  addCheck(fcn) {
53
43
  this.healthChecks.push(fcn);
54
44
  }
@@ -79,9 +69,12 @@ class HealthCheck {
79
69
  return await healthCallback();
80
70
  }));
81
71
 
82
- //let result = (result.reduce((e) => e.success, 200))
83
- let callId = (Math.random()*0xFFFFFF<<0).toString(16);
84
- this.lastCallId = callId;
72
+ let callId = ++this.lastCallId;
73
+
74
+ // start the counter over
75
+ if ((callId % (1 << 30)) === 0) {
76
+ callId = this.lastCallId = 1;
77
+ }
85
78
 
86
79
  let status = this.defaultResponse.status;
87
80
 
@@ -94,10 +87,9 @@ class HealthCheck {
94
87
 
95
88
  // set this in the header no matter what
96
89
  res.setHeader('check', callId)
97
- res.setHeader('lastCommit', this.lastCommit)
98
90
 
99
91
  if (checkFormat === ".json") {
100
- const payload = { "check": callId, "commit": this.lastCommit }
92
+ const payload = { "check": callId }
101
93
 
102
94
  if (status === 200) {
103
95
  res.status(status).json({
@@ -125,7 +117,7 @@ class HealthCheck {
125
117
  }
126
118
  } catch(error) {
127
119
  res.status(500).send();
128
- }
120
+ }
129
121
  }
130
122
  }
131
123
 
@@ -1,38 +1,99 @@
1
1
  const winston = require('winston');
2
- const logger = new winston.Logger();
3
- const loggerName = "console.debug";
4
-
5
- logger.add(winston.transports.Console, {
6
- name: loggerName,
7
- colorize: true,
8
- showLevel: true,
9
- level: 'debug'
2
+ const jsonStringify = require('fast-safe-stringify');
3
+ const colorizer = winston.format.colorize();
4
+
5
+ require('winston-logstash');
6
+
7
+ const devFormat = {
8
+ transform(info) {
9
+ const { message } = info;
10
+ const level = info[Symbol.for('level')];
11
+ const args = info[Symbol.for('splat')];
12
+ const strArgs = args.map(jsonStringify).join(' ');
13
+ info[Symbol.for('message')] = colorizer.colorize(level, `${level}: ${message} ${strArgs}`);
14
+ return info;
15
+ }
16
+ };
17
+
18
+ const defaultTransport = new winston.transports.Console({
19
+ format: devFormat
20
+ });
21
+
22
+ const logger = winston.createLogger({
23
+ level: 'debug',
24
+ defaultMeta: { },
25
+ transports: [defaultTransport]
10
26
  });
11
27
 
12
28
  class S4Logger {
29
+ configure(options) {
30
+ if (!options) {
31
+ return;
32
+ }
33
+
34
+ if (options.level && ["debug", "info", "error", "warn"].indexOf(options.level.toLowerCase())) {
35
+ logger.level = options.level;
36
+ }
37
+
38
+ logger.defaultMeta.service = options.service;
39
+ switch(options.format.toLowerCase()) {
40
+ case "logstash":
41
+ defaultTransport.format = winston.format.logstash();
42
+ break;
43
+ case "json":
44
+ defaultTransport.format = winston.format.json();
45
+ break;
46
+ default:
47
+ defaultTransport.format = devFormat;
48
+ break;
49
+ }
50
+ }
51
+
13
52
  setLevel(level) {
14
53
  if (level && ["debug", "info", "error", "warn"].indexOf(level.toLowerCase())) {
15
- logger.transports[loggerName].level = level;
54
+ logger.level = level;
16
55
  }
17
56
  }
57
+
18
58
  mute() {
19
- logger.transports[loggerName].silent = true;
59
+ logger.transports.forEach(t => {
60
+ t.silent = true;
61
+ });
20
62
  }
63
+
21
64
  unmute() {
22
- logger.transports[loggerName].silent = false;
65
+ logger.transports.forEach(t => {
66
+ t.silent = false;
67
+ });
23
68
  }
69
+
24
70
  debug(str, args) {
25
71
  logger.log("debug", str, args);
26
72
  }
73
+
74
+ log(str, args) {
75
+ logger.log("debug", str, args);
76
+ }
77
+
27
78
  warn(str, args) {
28
79
  logger.log("warn", str, args);
29
80
  }
81
+
30
82
  info(str, args) {
31
83
  logger.log("info", str, args);
32
84
  }
85
+
33
86
  error(str, args) {
34
87
  logger.log("error", str, args);
35
88
  }
89
+
90
+ getStream() {
91
+ return {
92
+ write: function(message, encoding){
93
+ logger.info(message);
94
+ }
95
+ }
96
+ }
36
97
  }
37
98
 
38
99
  module.exports = new S4Logger();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.0.13",
3
+ "version": "1.0.16",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,7 +26,9 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "async": "^2.6.1",
29
+ "fast-safe-stringify": "^2.0.6",
29
30
  "git-rev": "^0.2.1",
30
- "winston": "^2.4.2"
31
+ "winston": "^3.2.1",
32
+ "winston-logstash": "^0.4.0"
31
33
  }
32
34
  }
package/package.tar.gz CHANGED
Binary file