Browse Source

Initial commit

master
ポール ウェッブ 10 months ago
parent
commit
6a70e34293
6 changed files with 253 additions and 2 deletions
  1. 7
    0
      .gitignore
  2. 15
    0
      LICENSE
  3. 26
    2
      README.md
  4. 78
    0
      cli.js
  5. 74
    0
      index.js
  6. 53
    0
      package.json

+ 7
- 0
.gitignore View File

@@ -0,0 +1,7 @@
1
+# Files
2
+.DS_Store
3
+package-lock.json
4
+Thumbs.db
5
+
6
+# Folders
7
+node_modules

+ 15
- 0
LICENSE View File

@@ -0,0 +1,15 @@
1
+BSD 3-Clause License
2
+
3
+Copyright (c) 2018, LBRY Inc.
4
+All rights reserved.
5
+
6
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
+
8
+* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
+
10
+* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11
+
12
+* Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13
+
14
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
+

+ 26
- 2
README.md View File

@@ -1,3 +1,27 @@
1
-# logger
1
+# @inc/logger
2 2
 
3
-A logging module to unify logging across services.
3
+[![INC](https://img.shields.io/badge/%F0%9F%92%A1-IdeasNeverCease/logger-51dcfb.svg?style=flat-square)](https://git.inc.sh/IdeasNeverCease/logger)
4
+
5
+> A logging module to unify logging across services.
6
+
7
+
8
+
9
+### Installation
10
+
11
+```bash
12
+$ npm i @inc/logger -S
13
+```
14
+
15
+
16
+
17
+### Usage
18
+
19
+```js
20
+const log = require("@inc/logger")("NameOfYourApp");
21
+```
22
+
23
+
24
+
25
+### License
26
+
27
+[BSD-3-Clause](LICENSE) Copyright © [Ideas Never Cease](https://inc.sh)

+ 78
- 0
cli.js View File

@@ -0,0 +1,78 @@
1
+#!/usr/bin/env node
2
+"use strict";
3
+
4
+
5
+
6
+//  P A C K A G E S
7
+
8
+const colors = require("colorette");
9
+const { pretty } = require("pino");
10
+const program = require("commander");
11
+const pump = require("pump");
12
+const split = require("split2");
13
+const through = require("through2");
14
+
15
+//  U T I L S
16
+
17
+program
18
+  .description("Pipe heroku live tail logs into a readable output")
19
+  .option("-i, --include [include]", "Comma-delimited list of properties to include. Defaults to all")
20
+  .parse(process.argv);
21
+
22
+const props = program.include ? program.include.split(",") : [];
23
+const regex = /^.*?:\s({.*})$/;
24
+
25
+const transformer = through.obj((chunk, enc, cb) => {
26
+  const [, herokuSyslog, message] = chunk.match(/^(.*?]:)(\s.*)$/);
27
+
28
+  // Non-JSON logs
29
+  if (!regex.test(chunk)) {
30
+    return cb(null, `${colors.green(herokuSyslog)}${message}\n`);
31
+  }
32
+
33
+  // Parse JSON
34
+  let parsed;
35
+
36
+  try {
37
+    parsed = JSON.parse(chunk.match(regex)[1]);
38
+  } catch (e) {
39
+    // Not a valid JSON line, log it as is
40
+    return cb(null, `${colors.green(herokuSyslog)}${message}\n`);
41
+  }
42
+
43
+  // Cherry picked default properties
44
+  const defaultProps = {
45
+    time: parsed.time,
46
+    level: parsed.level,
47
+    name: parsed.name,
48
+    v: parsed.v,
49
+    msg: parsed.msg,
50
+    namespace: parsed.namespace
51
+  };
52
+
53
+  const extraProps = !props.length ?
54
+    // Include all properties if user hasn't chosen any
55
+    parsed :
56
+    // Included properties by user
57
+    props.reduce((acc, prop) => ({
58
+      ...acc,
59
+      [prop]: parsed[prop]
60
+    }), {});
61
+
62
+  // Stringify JSON
63
+  const stringified = JSON.stringify({ ...defaultProps, ...extraProps });
64
+
65
+  return cb(null, `${stringified}\n`);
66
+});
67
+
68
+
69
+
70
+//  P R O G R A M
71
+
72
+pump( // Pipeline steps
73
+  process.stdin, // Take everything that is piped in
74
+  split(), // Split stream so we have a line per chunk
75
+  transformer, // Apply our own transformer
76
+  pretty, // Pretty print JSON lines through Pino
77
+  process.stdout // Output everything to stdout
78
+);

+ 74
- 0
index.js View File

@@ -0,0 +1,74 @@
1
+"use strict";
2
+
3
+
4
+
5
+//  P A C K A G E S
6
+
7
+const _ = require("lodash");
8
+const pino = require("pino");
9
+
10
+const logger = pino({
11
+  base: {
12
+    name: process.env.APP_NAME
13
+  },
14
+  level: process.env.NODE_ENV === "test" ? "silent" : "info",
15
+  prettyPrint: process.env.NODE_ENV === "development",
16
+  serializers: {
17
+    e: pino.stdSerializers.err,
18
+    err: pino.stdSerializers.err,
19
+    error: pino.stdSerializers.err,
20
+    req: pino.stdSerializers.req,
21
+    res: pino.stdSerializers.res
22
+  }
23
+});
24
+
25
+
26
+
27
+//  P R O G R A M
28
+
29
+function logWrapper(log, level, msg, ...args) {
30
+  switch(true) {
31
+    case (!args.length):
32
+      log[level](msg);
33
+      break;
34
+
35
+    case (args.length > 1):
36
+      log[level](msg, ...args);
37
+      break;
38
+
39
+    case (_.isPlainObject(args[0]) || args[0] instanceof Error):
40
+      log[level](args[0], msg);
41
+      break;
42
+
43
+    case (_.isArray(args[0])):
44
+      log[level]({ array: args[0] }, msg);
45
+      break;
46
+
47
+    default:
48
+      log[level](msg, args[0]);
49
+      break;
50
+  }
51
+}
52
+
53
+
54
+
55
+//  E X P O R T
56
+
57
+module.exports = exports = name => {
58
+  const log = logger.child({ namespace: name });
59
+
60
+  return {
61
+    info(msg, ...args) {
62
+      logWrapper(log, "info", msg, ...args);
63
+    },
64
+
65
+    warn(msg, ...args) {
66
+      logWrapper(log, "warn", msg, ...args);
67
+    },
68
+
69
+    error(msg, ...args) {
70
+      logWrapper(log, "error", msg, ...args);
71
+    }
72
+  };
73
+};
74
+

+ 53
- 0
package.json View File

@@ -0,0 +1,53 @@
1
+{
2
+  "author": {
3
+    "email": "paul@inc.sh",
4
+    "name": "Paul Anthony Webb"
5
+  },
6
+  "bugs": {
7
+    "url": "https://hub.socii.network/inc"
8
+  },
9
+  "dependencies": {
10
+    "colorette": "^1.0.7",
11
+    "commander": "^2.19.0",
12
+    "lodash": "^4.17.11",
13
+    "pino": "^5.9.0",
14
+    "pump": "^3.0.0",
15
+    "split2": "^3.0.0",
16
+    "through2": "^3.0.0"
17
+  },
18
+  "description": "A logging module to unify logging across services.",
19
+  "devDependencies": {
20
+    "@inc/eslint-config": "^1.1.2",
21
+    "eslint": "^5.9.0",
22
+    "husky": "^1.2.0",
23
+    "npm-run-all": "^4.1.5",
24
+    "snazzy": "^8.0.0",
25
+    "standardx": "^3.0.1",
26
+    "updates": "^5.4.2"
27
+  },
28
+  "engines": {
29
+    "node": ">=11.0.0"
30
+  },
31
+  "eslintConfig": {
32
+    "extends": "@inc"
33
+  },
34
+  "husky": {
35
+    "hooks": {
36
+      "pre-commit": "npm run format && git add -A :/"
37
+    }
38
+  },
39
+  "license": "BSD-3-Clause",
40
+  "main": "index.js",
41
+  "name": "@inc/logger",
42
+  "repository": {
43
+    "type": "git",
44
+    "url": "https://inc.sh/IdeasNeverCease/logger.git"
45
+  },
46
+  "scripts": {
47
+    "format": "eslint '**/*.js' --fix",
48
+    "test": "run-s test:*",
49
+    "test:dependencies": "updates -u ./",
50
+    "test:lint": "standardx --verbose | snazzy"
51
+  },
52
+  "version": "1.0.0"
53
+}

Loading…
Cancel
Save