-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a97f8ec
Showing
10 changed files
with
2,709 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Publish to NPM | ||
on: | ||
push: | ||
branches: [main] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: "18.x" | ||
registry-url: "https://registry.npmjs.org" | ||
- name: Install Dependencies | ||
run: yarn install | ||
- name: Run Tests | ||
run: yarn test | ||
- name: Build | ||
run: yarn build | ||
- name: Publish to NPM | ||
run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} |
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,4 @@ | ||
.build | ||
node_modules | ||
log | ||
.env.local |
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,3 @@ | ||
src | ||
node_modules | ||
log |
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,80 @@ | ||
# Logzly | ||
|
||
A simple logger. | ||
|
||
## Getting Started | ||
|
||
Install it: | ||
|
||
```bash | ||
yarn add logzly | ||
# or | ||
npm i logzly | ||
``` | ||
|
||
Then import it where you want to use it. | ||
|
||
```js | ||
import { log } from "logzly"; | ||
``` | ||
|
||
## Log Types | ||
|
||
The logger has several types with escalating log levels. Each log command requires a topic and optionally takes an object of data as the second argument. | ||
|
||
```js | ||
log.trace("Topic.Name", {}); | ||
log.debug("Topic.Name", {}); | ||
log.info("Topic.Name", {}); | ||
log.warn("Topic.Name", {}); | ||
log.error("Topic.Name", {}); | ||
log.fatal("Topic.Name", {}); | ||
``` | ||
|
||
## Log Levels | ||
|
||
The log levels are: | ||
|
||
- trace | ||
- debug | ||
- info | ||
- warn | ||
- error | ||
- fatal | ||
|
||
If you set your log level at a level above the log type you are logging, it will not be logged: | ||
|
||
```js | ||
log.setLogLevel("debug"); | ||
log.trace("Trace.Topic"); // Never logs | ||
log.debug("Debug.Topic"); // Logs | ||
log.info("Info.Topic"); // Logs | ||
``` | ||
|
||
You can also set the log level by setting the `LOG_LEVEL` environment variable, but `setLogLevel` will override it if used. | ||
|
||
## Filtering | ||
|
||
You may set a filter and exclude all logs that do not match the filter. | ||
|
||
```js | ||
log.setFilter("Foo.*"); | ||
log.info("Bar.Baz"); // Never logs | ||
log.info("Foo.Baz"); // Logs | ||
``` | ||
|
||
By default, the filter is set to `*` which allows all logs. You can also set this using the `LOG_FILTER` environment variable but `setFilter` will override it if used. | ||
|
||
## Metadata | ||
|
||
You can set any arbitrary meta data with `log.addMetaData()` | ||
|
||
```js | ||
log.addMetaData({ | ||
foo: "bar", | ||
}); | ||
|
||
log.trace("Foo.Bar"); // "Foo.Bar" { _meta: { foo: "bar" } } | ||
``` | ||
|
||
The `log.addMetaData()` will extend any previous metadata. `log.clearMetaData()` will reset the meta object to empty. |
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 @@ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
moduleFileExtensions: ["js", "json", "ts"], | ||
rootDir: "src", | ||
testRegex: ".spec.ts$", | ||
transform: { | ||
"^.+\\.(t|j)s$": "ts-jest", | ||
}, | ||
coverageDirectory: "../coverage", | ||
}; |
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,21 @@ | ||
{ | ||
"name": "logzly", | ||
"version": "1.0.0", | ||
"description": "A lightweight logging utility", | ||
"main": ".build/index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "jest", | ||
"test:watch": "jest --watchAll", | ||
"release": "yarn build && NPM_TOKEN=$(grep NPM_TOKEN .env.local | cut -d \"=\" -f2) npm publish" | ||
}, | ||
"author": "Tyson Cadenhead", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^22.5.3", | ||
"jest": "^29.7.0", | ||
"ts-jest": "^29.2.5", | ||
"typescript": "^5.5.4" | ||
} | ||
} |
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,185 @@ | ||
import { ILogger, Log } from ".."; | ||
|
||
const getLogger = () => { | ||
const testLogger: ILogger = { | ||
trace: jest.fn(), | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
fatal: jest.fn(), | ||
}; | ||
|
||
const logger = new Log(); | ||
|
||
logger.setLogger(testLogger); | ||
|
||
return { | ||
logger, | ||
testLogger, | ||
}; | ||
}; | ||
|
||
describe("Logger", () => { | ||
it("Should trace", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.trace("Test.Topic", { message: "test" }); | ||
expect(testLogger.trace).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should debug", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.debug("Test.Topic", { message: "test" }); | ||
expect(testLogger.debug).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log info", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.info("Test.Topic", { message: "test" }); | ||
expect(testLogger.info).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log warnings", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.warn("Test.Topic", { message: "test" }); | ||
expect(testLogger.warn).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log errors", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.error("Test.Topic", { message: "test" }); | ||
expect(testLogger.error).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log fatal errors", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.fatal("Test.Topic", { message: "test" }); | ||
expect(testLogger.fatal).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should not log trace if the topic is filtered out", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setFilter("Foo.*"); | ||
logger.trace("Bar.Baz", { message: "test" }); | ||
expect(testLogger.trace).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should log if the topic is not filtered out", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setFilter("Foo.*"); | ||
logger.trace("Foo.Bar", { message: "test" }); | ||
expect(testLogger.trace).toHaveBeenCalledWith("Foo.Bar", { | ||
message: "test", | ||
}); | ||
logger.setFilter("*"); | ||
}); | ||
|
||
it("Should not log trace if the log level is set to debug", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("debug"); | ||
logger.trace("Test.Topic", { message: "test" }); | ||
expect(testLogger.trace).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should not log debug if the log level is set to info", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("info"); | ||
logger.debug("Test.Topic", { message: "test" }); | ||
expect(testLogger.debug).not.toHaveBeenCalled(); | ||
}); | ||
it("Should not log info if the log level is set to warn", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("warn"); | ||
logger.info("Test.Topic", { message: "test" }); | ||
expect(testLogger.info).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should not log warn if the log level is set to error", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("error"); | ||
logger.warn("Test.Topic", { message: "test" }); | ||
expect(testLogger.warn).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should not log error if the log level is set to fatal", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("fatal"); | ||
logger.error("Test.Topic", { message: "test" }); | ||
expect(testLogger.error).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should log fatal if the log level is set to fatal", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("fatal"); | ||
logger.fatal("Test.Topic", { message: "test" }); | ||
expect(testLogger.fatal).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log error if the log level is set to error", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("error"); | ||
logger.error("Test.Topic", { message: "test" }); | ||
expect(testLogger.error).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log warn if the log level is set to warn", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("warn"); | ||
logger.warn("Test.Topic", { message: "test" }); | ||
expect(testLogger.warn).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log info if the log level is set to info", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("info"); | ||
logger.info("Test.Topic", { message: "test" }); | ||
expect(testLogger.info).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log debug if the log level is set to debug", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("debug"); | ||
logger.debug("Test.Topic", { message: "test" }); | ||
expect(testLogger.debug).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should log trace if the log level is set to trace", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.setLogLevel("trace"); | ||
logger.trace("Test.Topic", { message: "test" }); | ||
expect(testLogger.trace).toHaveBeenCalledWith("Test.Topic", { | ||
message: "test", | ||
}); | ||
}); | ||
|
||
it("Should include metadata", () => { | ||
const { logger, testLogger } = getLogger(); | ||
logger.addMetaData({ foo: "bar" }); | ||
logger.info("Test.Topic", {}); | ||
expect(testLogger.info).toHaveBeenCalledWith("Test.Topic", { | ||
_meta: { foo: "bar" }, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.