Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
feat(@phenomic/core): add an logger utility to normalize plugin logs …
Browse files Browse the repository at this point in the history
…output
  • Loading branch information
MoOx committed Jun 8, 2017
1 parent f86f253 commit f908a58
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/core/package.json
Expand Up @@ -25,6 +25,7 @@
],
"dependencies": {
"@phenomic/api-client": "^1.0.0-alpha.1",
"chalk": "^1.1.3",
"cosmiconfig": "^2.1.1",
"debug": "^2.6.0",
"express": "^4.14.0",
Expand All @@ -35,6 +36,7 @@
"level-sublevel": "^6.6.1",
"leveldown": "^1.5.0",
"levelup": "^1.3.3",
"log-symbols": "^1.0.2",
"mkdirp": "^0.5.1",
"path-to-regexp": "^1.7.0",
"rimraf": "^2.5.4",
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/logger/__tests__/__snapshots__/index.js.snap
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`log should log 1`] = `"ℹ sender test:  test msg"`;

exports[`log should log 2`] = `" sender test:  test debug"`;

exports[`log should log 3`] = `"ℹ sender test:  test info"`;

exports[`log should log 4`] = `"✔ sender test:  test success"`;

exports[`log should log 5`] = `"⚠ sender test:  test warning"`;

exports[`log should log 6`] = `"✖ sender test:  test error"`;
11 changes: 11 additions & 0 deletions packages/core/src/logger/__tests__/index.js
@@ -0,0 +1,11 @@
import logger from "..";

test("log should log", () => {
const log = logger("sender test", false);
expect(log("test msg")).toMatchSnapshot();
expect(log.debug("test debug")).toMatchSnapshot();
expect(log.info("test info")).toMatchSnapshot();
expect(log.success("test success")).toMatchSnapshot();
expect(log.warn("test warning")).toMatchSnapshot();
expect(log.error("test error")).toMatchSnapshot();
});
34 changes: 34 additions & 0 deletions packages/core/src/logger/index.js
@@ -0,0 +1,34 @@
import color from "chalk";
import logSymbols from "log-symbols";

const INFO = "info";
const SUCCESS = "success";
const WARN = "warning";
const ERROR = "error";

// from log-symbols
type TypeType = "info" | "success" | "warning" | "error" | null;
type WriterType = (msg: string) => void;

const log = (
sender: string,
symbol: TypeType,
message: string,
writer: WriterType
): string => {
const msg = `${symbol ? logSymbols[symbol] : " "} ${color.gray(sender + ": ")} ${message}`;
if (writer) {
writer(msg);
}
return msg;
};

export default (sender: string, writer: WriterType = console.log) => {
const logger = (msg: string) => log(sender, INFO, msg, writer);
logger.debug = (msg: string) => log(sender, null, msg, writer);
logger.info = (msg: string) => log(sender, INFO, msg, writer);
logger.success = (msg: string) => log(sender, SUCCESS, msg, writer);
logger.warn = (msg: string) => log(sender, WARN, msg, writer);
logger.error = (msg: string) => log(sender, ERROR, msg, writer);
return logger;
};

0 comments on commit f908a58

Please sign in to comment.