@peerbit/logger 1.0.0
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/LICENSE +24 -0
- package/lib/esm/index.d.ts +8 -0
- package/lib/esm/index.js +42 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/package.json +36 -0
- package/src/index.ts +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2018 shamb0t
|
|
4
|
+
Copyright (c) 2018 Haja Networks Oy
|
|
5
|
+
Copyright (c) 2020 dao.xyz
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
24
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare const getEnv: (key: any) => string | undefined;
|
|
3
|
+
export declare const getLogLevel: () => string | undefined;
|
|
4
|
+
declare const logger: (options?: {
|
|
5
|
+
module?: string;
|
|
6
|
+
level?: string;
|
|
7
|
+
}) => import("pino").Logger<import("pino").LoggerOptions | import("pino").DestinationStream>;
|
|
8
|
+
export { logger };
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import pino from "pino";
|
|
2
|
+
const isNode = typeof window === undefined || typeof window === "undefined";
|
|
3
|
+
export const getEnv = (key) => {
|
|
4
|
+
if (isNode) {
|
|
5
|
+
// node
|
|
6
|
+
return process.env[key];
|
|
7
|
+
}
|
|
8
|
+
// browser
|
|
9
|
+
return window.process?.env?.[key];
|
|
10
|
+
};
|
|
11
|
+
export const getLogLevel = () => {
|
|
12
|
+
const level = getEnv("LOG_LEVEL") || getEnv("REACT_APP_LOG_LEVEL");
|
|
13
|
+
if (!level) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const levels = ["fatal", "error", "warn", "info", "debug", "trace"];
|
|
17
|
+
if (levels.indexOf(level) === -1) {
|
|
18
|
+
throw new Error("Unexpected LOG_LEVEL: " +
|
|
19
|
+
level +
|
|
20
|
+
". Expecting one of: " +
|
|
21
|
+
JSON.stringify(levels));
|
|
22
|
+
}
|
|
23
|
+
return level;
|
|
24
|
+
};
|
|
25
|
+
const logger = (options) => {
|
|
26
|
+
let logger = pino();
|
|
27
|
+
if (options?.module) {
|
|
28
|
+
logger = logger.child({ module: options.module });
|
|
29
|
+
}
|
|
30
|
+
if (options?.level) {
|
|
31
|
+
logger.level = options.level;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const logLevel = getLogLevel();
|
|
35
|
+
if (logLevel) {
|
|
36
|
+
logger.level = logLevel;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return logger;
|
|
40
|
+
};
|
|
41
|
+
export { logger };
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,MAAM,GAAG,OAAO,MAAM,KAAK,SAAS,IAAI,OAAO,MAAM,KAAK,WAAW,CAAC;AAE5E,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,GAAG,EAAE,EAAE;IAC7B,IAAI,MAAM,EAAE;QACX,OAAO;QACP,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;KACxB;IACD,UAAU;IACV,OAAO,MAAM,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;AACnC,CAAC,CAAC;AACF,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,EAAE;IAC/B,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAAC,qBAAqB,CAAC,CAAC;IACnE,IAAI,CAAC,KAAK,EAAE;QACX,OAAO,SAAS,CAAC;KACjB;IACD,MAAM,MAAM,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACpE,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE;QACjC,MAAM,IAAI,KAAK,CACd,wBAAwB;YACvB,KAAK;YACL,sBAAsB;YACtB,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CACvB,CAAC;KACF;IACD,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,MAAM,GAAG,CAAC,OAA6C,EAAE,EAAE;IAChE,IAAI,MAAM,GAAG,IAAI,EAAE,CAAC;IACpB,IAAI,OAAO,EAAE,MAAM,EAAE;QACpB,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;KAClD;IACD,IAAI,OAAO,EAAE,KAAK,EAAE;QACnB,MAAM,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;KAC7B;SAAM;QACN,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAC;QAC/B,IAAI,QAAQ,EAAE;YACb,MAAM,CAAC,KAAK,GAAG,QAAQ,CAAC;SACxB;KACD;IACD,OAAO,MAAM,CAAC;AACf,CAAC,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peerbit/logger",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Logging utils",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"module": "lib/esm/index.js",
|
|
8
|
+
"types": "lib/esm/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./lib/esm/index.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"src",
|
|
15
|
+
"!src/**/__tests__",
|
|
16
|
+
"!lib/**/__tests__",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"pino": "^8.14.1"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"clean": "shx rm -rf lib/*",
|
|
27
|
+
"build": "yarn clean && tsc -p tsconfig.json",
|
|
28
|
+
"postbuild": "echo '{\"type\":\"module\"} ' | node ../../../node_modules/.bin/json > lib/esm/package.json",
|
|
29
|
+
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
|
|
30
|
+
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
|
|
31
|
+
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
|
|
32
|
+
},
|
|
33
|
+
"author": "dao.xyz",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"gitHead": "069ce2f62a76c342875a1cc695c6f210beff13fd"
|
|
36
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import pino from "pino";
|
|
2
|
+
|
|
3
|
+
const isNode = typeof window === undefined || typeof window === "undefined";
|
|
4
|
+
|
|
5
|
+
export const getEnv = (key) => {
|
|
6
|
+
if (isNode) {
|
|
7
|
+
// node
|
|
8
|
+
return process.env[key];
|
|
9
|
+
}
|
|
10
|
+
// browser
|
|
11
|
+
return window.process?.env?.[key];
|
|
12
|
+
};
|
|
13
|
+
export const getLogLevel = () => {
|
|
14
|
+
const level = getEnv("LOG_LEVEL") || getEnv("REACT_APP_LOG_LEVEL");
|
|
15
|
+
if (!level) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const levels = ["fatal", "error", "warn", "info", "debug", "trace"];
|
|
19
|
+
if (levels.indexOf(level) === -1) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"Unexpected LOG_LEVEL: " +
|
|
22
|
+
level +
|
|
23
|
+
". Expecting one of: " +
|
|
24
|
+
JSON.stringify(levels)
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return level;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const logger = (options?: { module?: string; level?: string }) => {
|
|
31
|
+
let logger = pino();
|
|
32
|
+
if (options?.module) {
|
|
33
|
+
logger = logger.child({ module: options.module });
|
|
34
|
+
}
|
|
35
|
+
if (options?.level) {
|
|
36
|
+
logger.level = options.level;
|
|
37
|
+
} else {
|
|
38
|
+
const logLevel = getLogLevel();
|
|
39
|
+
if (logLevel) {
|
|
40
|
+
logger.level = logLevel;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return logger;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export { logger };
|