Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add consola support #24

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-trains-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"loglayer": minor
---

Add consola support
31 changes: 26 additions & 5 deletions 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` / `log4js-node` / `electron-log` / `signale` / etc).
(`bunyan` / `winston` / `pino` / `roarr` / `log4js-node` / `electron-log` / `signale` / `consola` / etc).

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

Expand Down Expand Up @@ -53,14 +53,15 @@ logLayer

- [Installation](#installation)
- [Example installations](#example-installations)
- [`console`](#console)
- [`pino`](#pino)
- [`bunyan`](#bunyan)
- [`winston`](#winston)
- [`roarr`](#roarr)
- [`consola`](#consola)
- [`console`](#console)
- [`electron-log`](#electron-log)
- [`log4js-node`](#log4js-node)
- [`pino`](#pino)
- [`roarr`](#roarr)
- [`signale`](#signale)
- [`winston`](#winston)
- [Example integration](#example-integration)
- [API](#api)
- [Constructor](#constructor)
Expand Down Expand Up @@ -291,6 +292,26 @@ const s = log.getLoggerInstance()
s.success('Operation successful');
```

### `consola`

[consola docs](https://github.com/unjs/consola)

- The default log level is `3` which excludes `debug` and `trace`. Set to `5` for both.

```typescript
import { LogLayer, LoggerType } from 'loglayer'
import { type ConsolaInstance, createConsola } from "consola";

const log = new LogLayer<ConsolaInstance>({
logger: {
instance: createConsola({
level: 5
}),
type: LoggerType.CONSOLA,
},
})
```

## Example integration

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

const consola = createConsola({
level: 5,
});

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

testMethods(log);
16 changes: 16 additions & 0 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"livetest:winston": "ts-node-dev livetests/winston.ts",
"livetest:log4js-node": "ts-node-dev livetests/log4js-node.ts",
"livetest:signale": "ts-node-dev livetests/signale.ts",
"livetest:consola": "ts-node-dev livetests/consola.ts",
"prepare": "husky install",
"release": "changeset publish",
"test": "vitest run",
Expand Down Expand Up @@ -70,7 +71,8 @@
"abstraction",
"pino",
"electron",
"signale"
"signale",
"consola"
],
"devDependencies": {
"@biomejs/biome": "^1.6.4",
Expand All @@ -83,6 +85,7 @@
"@types/roarr": "^2.14.3",
"@types/signale": "^1.4.7",
"bunyan": "^1.8.15",
"consola": "^3.2.3",
"express": "^4.19.2",
"husky": "^8.0.0",
"lint-staged": "^15.2.2",
Expand Down
1 change: 1 addition & 0 deletions src/LogLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ export class LogLayer<ExternalLogger extends LoggerLibrary = LoggerLibrary, Erro

if (d && hasObjData) {
switch (this.loggerType) {
case LoggerType.CONSOLA:
case LoggerType.ELECTRON_LOG:
case LoggerType.LOG4JS_NODE:
case LoggerType.SIGNALE:
Expand Down
94 changes: 94 additions & 0 deletions src/__tests__/consola.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { type ConsolaInstance, consola } from "consola";
import { beforeAll, beforeEach, describe, expect, it, vitest } from "vitest";
import { LogLayer } from "../LogLayer";
import { LoggerType } from "../types";

let log: LogLayer<ConsolaInstance>;

describe("structured logger with consola", () => {
beforeAll(() => {
// consola.wrapAll();

log = new LogLayer<ConsolaInstance>({
logger: {
instance: consola,
type: LoggerType.CONSOLA,
},
});
});

beforeEach(() => {
// Re-mock consola before each test call to remove
// calls from before
consola.mockTypes(() => vitest.fn());
});

it("should log a message", () => {
log.info("this is a test message");
// @ts-ignore
const consolaMessages = consola.info.mock.calls.map((c) => c[0]);
expect(consolaMessages).toContain("this is a test message");
});

it("should log a message with a prefix", () => {
log.withPrefix("[testing]").info("this is a test message");
// @ts-ignore
const consolaMessages = consola.info.mock.calls.map((c) => c[0]);
expect(consolaMessages).toContain("[testing] this is a test message");
});

it("should include context", () => {
log.withContext({
sample: "data",
});

log.info("this is a test message");
// @ts-ignore
const msg = consola.info.mock.calls[0][0];
expect(msg).toContain("this is a test message");
// @ts-ignore
const data = consola.info.mock.calls[0][1];
expect(data).toMatchObject({
sample: "data",
});
});

it("should include metadata", () => {
log.withContext({
test: "context",
});

log
.withMetadata({
meta: "data",
})
.info("this is a test message");

// @ts-ignore
const msg = consola.info.mock.calls[0][0];
expect(msg).toContain("this is a test message");
// @ts-ignore
const data = consola.info.mock.calls[0][1];
expect(data).toMatchObject({
sample: "data",
test: "context",
});
});

it("should include an error", () => {
log.withContext({
test: "context",
});

log.withError(new Error("err")).info("this is a test message");
// @ts-ignore
const msg = consola.info.mock.calls[0][0];
expect(msg).toContain("this is a test message");
// @ts-ignore
const data = consola.info.mock.calls[0][1];
expect(data).toMatchObject({
test: "context",
});
expect(data.err).toBeDefined();
});
});
1 change: 1 addition & 0 deletions src/types/common.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export enum LoggerType {
BUNYAN = "bunyan",
LOG4JS_NODE = "log4js-node",
SIGNALE = "signale",
CONSOLA = "consola",
CONSOLE = "console",
}

Expand Down
Loading