Skip to content

Commit

Permalink
feat: log4js-node support (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
theogravity committed May 6, 2024
1 parent 50bcfc0 commit c3b5468
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .changeset/shiny-teachers-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"loglayer": minor
---

Add [`log4js-node`](https://github.com/log4js-node/log4js-node) support

See README.md for more details.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)

Standardize the way you write logs with the `loglayer` abstraction using your existing logging library
(`bunyan` / `winston` / `pino` / `roarr` / `electron-log` / etc).
(`bunyan` / `winston` / `pino` / `roarr` / `log4js-node` / `electron-log` / etc).

Spend less time from having to *define* your logs and spend more writing them.

Expand Down Expand Up @@ -59,6 +59,7 @@ logLayer
- [`winston`](#winston)
- [`roarr`](#roarr)
- [`electron-log`](#electron-log)
- [`log4js-node`](#log4js-node)
- [Example integration](#example-integration)
- [API](#api)
- [Constructor](#constructor)
Expand Down Expand Up @@ -234,6 +235,35 @@ const logger = new LogLayer({
});
```

### `log4js-node`

[log4js-node docs](https://log4js-node.github.io/log4js-node/index.html)

- `log4js-node` only works in `node.js` and not the browser
- By default, `log4js-node` logging is disabled and must be configured via `level` or advanced configuration
- You may want to use a custom [layout](https://log4js-node.github.io/log4js-node/layouts.html) as `log4js-node` prints only
string output by default
- Use [winston](#winston) as an alternative to `log4js-node` if the configuration for `log4js-node` is too complex

```typescript
import { LogLayer, LoggerType } from 'loglayer'
import log4js from 'log4js'

const log4jsInstance = log4js.getLogger()

// Enable logging output
// note: You'll most likely want to use appenders and a custom layout
// instead to write your logs in log4js-node
log4jsInstance.level = "trace";

const log = new LogLayer({
logger: {
instance: log4jsInstance,
type: LoggerType.LOG4JS_NODE,
},
})
```

## Example integration

Using `express` and `pino`:
Expand Down
15 changes: 15 additions & 0 deletions livetests/log4js-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import log4js from "log4js";
import { LogLayer, LoggerType } from "../src";
import { testMethods } from "./utils";

const log4jsInstance = log4js.getLogger();
log4jsInstance.level = "trace";

const log = new LogLayer({
logger: {
instance: log4jsInstance,
type: LoggerType.LOG4JS_NODE,
},
});

testMethods(log);
131 changes: 110 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"livetest:pino": "ts-node-dev livetests/pino.ts",
"livetest:roarr": "ROARR_LOG=true ts-node-dev livetests/roarr.ts",
"livetest:winston": "ts-node-dev livetests/winston.ts",
"livetest:log4js": "ts-node-dev livetests/log4js-node.ts",
"prepare": "husky install",
"release": "changeset publish",
"test": "vitest run",
Expand All @@ -56,6 +57,8 @@
"logging",
"log",
"roarr",
"log4js",
"log4js-node",
"winston",
"bunyan",
"structured",
Expand All @@ -80,15 +83,16 @@
"bunyan": "^1.8.15",
"express": "^4.19.2",
"husky": "^8.0.0",
"log4js": "^6.9.1",
"lint-staged": "^15.2.2",
"pino": "^8.0.0",
"roarr": "^7.11.0",
"serialize-error": "8.1.0",
"toc-md-alt": "^0.4.6",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typescript": "^5.4.5",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"vitest": "^1.6.0",
"winston": "^3.7.2"
},
Expand Down
Loading

0 comments on commit c3b5468

Please sign in to comment.