This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@phenomic/core): add an logger utility to normalize plugin logs …
…output
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/core/src/logger/__tests__/__snapshots__/index.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`log should log 1`] = `"[34mℹ[39m [90msender test: [39m test msg"`; | ||
|
||
exports[`log should log 2`] = `" [90msender test: [39m test debug"`; | ||
|
||
exports[`log should log 3`] = `"[34mℹ[39m [90msender test: [39m test info"`; | ||
|
||
exports[`log should log 4`] = `"[32m✔[39m [90msender test: [39m test success"`; | ||
|
||
exports[`log should log 5`] = `"[33m⚠[39m [90msender test: [39m test warning"`; | ||
|
||
exports[`log should log 6`] = `"[31m✖[39m [90msender test: [39m test error"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |