Skip to content

Commit

Permalink
docs: added usage with nuxt
Browse files Browse the repository at this point in the history
fix: exported LokiOptions
fix: improved error logging when a fetch failed
  • Loading branch information
Niki2k1 committed Mar 9, 2024
1 parent 5872085 commit b2a565a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { LokiReporter } from "consola-loki";
const { LokiReporter } = require("consola-loki");
```

Example:
## Example:
```ts
const consola = createConsola({
reporters: [
Expand All @@ -52,6 +52,39 @@ const consola = createConsola({
});
```

## With Nuxt:
Install `consola` and `@nuxt/kit`.

```ts
// modules/loki.ts
import { defineNuxtModule } from '@nuxt/kit';
import { consola } from 'consola';
import { LokiReporter, LokiOptions } from 'consola-loki';

export default defineNuxtModule<LokiOptions>({
meta: {
name: 'loki',
},
setup(options, nuxt) {
const loki = new LokiReporter(options);
consola.addReporter(loki);
},
});
```

```ts
// nuxt.config.ts
export default defineNuxtConfig({
// ...
loki: {
baseURL: 'your base url here',
user: '123456',
token: 'glc_......',
}
//...
})
```

## Development

- Clone this repository
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export { LokiReporter } from "./reporters/loki";

export interface LokiOptions {
baseURL: string;
user?: string;
token?: string;
labels?: Record<string, string>;
}
8 changes: 6 additions & 2 deletions src/reporters/loki.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $fetch, FetchError } from "ofetch";
import type { LogObject, ConsolaReporter } from "consola";
import { LokiOptions } from "../types";
import type { LokiOptions } from "../index";

// Mapping from consola log levels to loki log levels
const LogLevel = {
Expand Down Expand Up @@ -55,7 +55,11 @@ export class LokiReporter implements ConsolaReporter {
this.buffer = [];
} catch (error) {
if (error instanceof FetchError) {
console.error(error.data);
if (error.data) {
console.error(`error pushing logs to loki: ${error.data}`);
} else {
console.error(error);
}
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/types.ts

This file was deleted.

0 comments on commit b2a565a

Please sign in to comment.