Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Use eslint-config-exp (#57)
Browse files Browse the repository at this point in the history
* Use eslint-config-exp

* Cleanup

* Cleanup
  • Loading branch information
varney authored Jul 27, 2023
1 parent d6b6ad2 commit a10d7dc
Show file tree
Hide file tree
Showing 20 changed files with 1,539 additions and 1,851 deletions.
86 changes: 7 additions & 79 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,81 +1,9 @@
{
"parserOptions": {
"ecmaVersion": 2020
},
"env": {
"node": true,
"es6": true
},
"extends": ["eslint:recommended", "plugin:json/recommended"],
"plugins": ["json", "prettier"],
"rules": {
"prettier/prettier": "error",
"arrow-parens": 2,
"arrow-spacing": 2,
"brace-style": [2, "1tbs", {"allowSingleLine": false}],
"callback-return": 2,
"camelcase": 1,
"comma-spacing": 1,
"curly": [2, "multi-line"],
"dot-notation": [2, {"allowKeywords": true}],
"eol-last": 2,
"eqeqeq": 2,
"handle-callback-err": 2,
"key-spacing": [1, {"beforeColon": false, "afterColon": true}],
"keyword-spacing": 1,
"new-cap": 2,
"new-parens": 1,
"no-alert": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-catch-shadow": 2,
"no-console": 2,
"no-duplicate-imports": 1,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-parens": [1, "functions"],
"no-implied-eval": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-loop-func": 2,
"no-multi-spaces": 1,
"no-multi-str": 2,
"no-multiple-empty-lines": 1,
"no-native-reassign": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-symbol": 2,
"no-new-wrappers": 2,
"no-octal-escape": 2,
"no-path-concat": 2,
"no-process-exit": 2,
"no-proto": 2,
"no-return-assign": 2,
"no-script-url": 2,
"no-sequences": 2,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-trailing-spaces": 1,
"no-underscore-dangle": 0,
"no-unused-expressions": 2,
"no-use-before-define": [2, "nofunc"],
"no-var": 2,
"no-with": 2,
"object-curly-spacing": 0,
"prefer-arrow-callback": 2,
"prefer-const": ["error", {"destructuring": "all"}],
"prefer-template": 1,
"semi-spacing": [1, {"before": false, "after": true}],
"space-before-blocks": 1,
"space-infix-ops": 1,
"space-unary-ops": [1, {"words": true, "nonwords": false}],
"strict": [2, "global"],
"require-await": ["error"],
"yoda": [1, "never"]
}
"root": true,
"extends": [
"exp"
],
"plugins": [
"json"
]
}
10 changes: 5 additions & 5 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require:
- 'mocha-cakes-2'
- './test/config/setup.js'
timeout: 2000
reporter: 'spec'
- "mocha-cakes-2"
- "./test/config/setup.js"
timeout: 3000
reporter: "spec"
recursive: true
ui: 'mocha-cakes-2'
ui: "mocha-cakes-2"
5 changes: 3 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"bracketSpacing": false,
"bracketSpacing": true,
"printWidth": 120,
"arrowParens": "always",
"trailingComma": "none"
"trailingComma": "es5",
"semi": true
}
10 changes: 4 additions & 6 deletions config/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ const levels = {
warning: 4,
notice: 5,
info: 6,
debug: 7
};
const aliases = {
critical: "crit"
debug: 7,
};
const aliases = { critical: "crit" };

const colors = {
alert: "darkred",
Expand All @@ -20,11 +18,11 @@ const colors = {
warning: "yellow",
notice: "magenta",
info: "green",
debug: "gray"
debug: "gray",
};

module.exports = {
levels,
aliases,
colors
colors,
};
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const appConfig = require("exp-config");
const fs = require("fs");
const path = require("path");
const winston = require("winston");
const {format} = winston;
const { format } = winston;

const {getLoc} = require("./lib/get-loc");
const { getLoc } = require("./lib/get-loc");
const logLevels = require("./config/levels");
const splatEntry = require("./lib/splat-entry");
const cleanEntry = require("./lib/clean-entry");
Expand Down Expand Up @@ -52,7 +52,7 @@ function truncateTooLong(info) {
function metaDataFormat(info) {
const meta = info.metaData && info.metaData.meta;
if (!meta && Object.keys(info.metaData).length > 0) {
info.metaData = {meta: info.metaData};
info.metaData = { meta: info.metaData };
}
return info;
}
Expand All @@ -61,25 +61,23 @@ function defaultFormatter() {
return format.printf((info) => {
const meta = Object.keys(info).reduce(
(acc, key) => {
if (!["message", "metaData", "level"].includes(key)) {
if (![ "message", "metaData", "level" ].includes(key)) {
acc[key] = info[key];
}
return acc;
},
{...info.metaData}
{ ...info.metaData }
);

return `${info.timestamp} - ${info.level}: ${info.message}\t${stringify(meta)}`;
});
}

const transports = [new PromTransport()];
const transports = [ new PromTransport() ];

if (config.log === "file") {
transports.push(
new winston.transports.File({
filename: logFilename()
})
new winston.transports.File({ filename: logFilename() })
);
}

Expand All @@ -97,11 +95,11 @@ const logger = winston.createLogger({
level: config.logLevel || "info",
levels: logLevels.levels,
colors: logLevel.colors,
transports: transports,
exceptionHandlers: [new winston.transports.Console()],
transports,
exceptionHandlers: [ new winston.transports.Console() ],
exitOnError: appConfig.envName !== "production",
format: format.combine(
format.metadata({key: "metaData"}),
format.metadata({ key: "metaData" }),
format(splatEntry)(),
format(truncateTooLong)(),
format(cleanEntry)(),
Expand All @@ -110,10 +108,10 @@ const logger = winston.createLogger({
format(location)(),
format(metaDataFormat)(),
formatter
)
),
});

module.exports = {
logger,
buildLogger: logger.child.bind(logger)
buildLogger: logger.child.bind(logger),
};
10 changes: 6 additions & 4 deletions lib/get-loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ function getLoc(depth) {
};

stack = new Error().stack;
depth = !depth || isNaN(depth) ? 1 : depth > stack.length - 2 ? stack.length - 2 : depth;
if (!depth || isNaN(depth)) {
depth = 1;
} else {
depth = depth > stack.length - 2 ? stack.length - 2 : depth;
}
stack = stack.slice(depth + 1);

do {
Expand All @@ -33,6 +37,4 @@ function getLoc(depth) {
}
}

module.exports = {
getLoc
};
module.exports = { getLoc };
4 changes: 2 additions & 2 deletions lib/prom-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class PromTransport extends Transport {
this.logCounter = new prometheusClient.Counter({
name: logCounterName,
help: "Counts number of logs with loglevel as label",
labelNames: ["level", "eventName"]
labelNames: [ "level", "eventName" ],
});
}
}
log(info, callback) {
const metricLabels = {level: info.level};
const metricLabels = { level: info.level };
if (info.level === "error") {
const eventName = getEventName(info.metaData);
if (eventName) {
Expand Down
4 changes: 2 additions & 2 deletions lib/splat-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const isEqual = require("lodash.isequal");

function splatEntry(info) {
const splat = info[Symbol.for("splat")];
const message = splat ? [...splat] : [];
const message = splat ? [ ...splat ] : [];

if (isMetaData(info.message, message)) {
if (isEmpty(info.metaData) || includes(info.metaData, message)) {
Expand All @@ -19,7 +19,7 @@ function splatEntry(info) {

function isMetaData(infoMessage, message) {
// The last part can be part of the message if the message is formatted using %j or %o or something
const copy = [...message];
const copy = [ ...message ];
const last = copy.pop();

if (typeof last !== "object") {
Expand Down
Loading

0 comments on commit a10d7dc

Please sign in to comment.