Skip to content

Commit

Permalink
TELESTION-473 Use DATA_DIR in logger sample (#421)
Browse files Browse the repository at this point in the history
Use the configurations data directory instead of the PWD to save the log messages.
  • Loading branch information
pklaschka authored Jan 26, 2024
2 parents d50fc5b + 70f91ec commit 32b1333
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
8 changes: 6 additions & 2 deletions backend-deno/samples/logger/mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { startService } from "https://deno.land/x/telestion/mod.ts";
import { encode } from "https://deno.land/[email protected]/encoding/hex.ts";
import { resolve } from "https://deno.land/[email protected]/path/mod.ts";

const encoder = new TextEncoder();

const { messageBus } = await startService();
const { messageBus, dataDir } = await startService();

const logFilePath = resolve(dataDir, "log.txt");
await Deno.mkdir(dataDir, { recursive: true });

const logMessages = messageBus.subscribe("log.>");

Expand All @@ -17,7 +21,7 @@ for await (const msg of logMessages) {

console.log(`${currentTime} [${subject}] ${logMessage}`);
await Deno.writeFile(
"log.txt",
logFilePath,
encoder.encode(`${currentTime} [${subject}] ${logMessage}\n`),
{ append: true },
);
Expand Down
26 changes: 19 additions & 7 deletions docs/docs/Backend Development/typescript/e2e-log-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This tutorial will explain step-by-step how to write a log service that will lis
```ts
import { startService } from "https://deno.land/x/telestion/mod.ts";
import { encode } from "https://deno.land/[email protected]/encoding/hex.ts";
import { resolve } from "https://deno.land/[email protected]/path/mod.ts";
```

2. Next, we create a new TextEncoder instance. This will be used to turn messages into a format that can be written to a file.
Expand All @@ -27,16 +28,23 @@ This tutorial will explain step-by-step how to write a log service that will lis
3. We then call the `startService` function to set up our service. This will return an object containing information about the message bus that we can use to subscribe to messages.

```ts
const { messageBus } = await startService();
const { messageBus, dataDir } = await startService();
```

4. We then subscribe to the message bus, using a wildcard subscription for any messages published on the `log.>` subject. This will allow us to receive all messages published on any topics starting with `log.`.
4. We then resolve the log file path and create its parent directory if it doesn't exist yet.

```ts
const logFilePath = resolve(dataDir, "log.txt");
await Deno.mkdir(dataDir, { recursive: true });
```

5. We then subscribe to the message bus, using a wildcard subscription for any messages published on the `log.>` subject. This will allow us to receive all messages published on any topics starting with `log.`.

```ts
const logMessages = messageBus.subscribe("log.>");
```

5. We use a for-await-of loop to receive messages from the message bus. For each message, we extract the subject (split the string on `.`, then take the second element) and the message data, which we encode using the `encode` function from the standard library.
6. We use a for-await-of loop to receive messages from the message bus. For each message, we extract the subject (split the string on `.`, then take the second element) and the message data, which we encode using the `encode` function from the standard library.

```ts
for await (const msg of logMessages) {
Expand All @@ -46,12 +54,12 @@ This tutorial will explain step-by-step how to write a log service that will lis
const subject = msg.subject.split(".")[1];
```

6. We log the message to the console and write it to a file (appending it to the end).
7. We log the message to the console and write it to a file (appending it to the end).

```ts
console.log(`${currentTime} [${subject}] ${logMessage}`);
await Deno.writeFile(
"log.txt",
logFilePath,
encoder.encode(`${currentTime} [${subject}] ${logMessage}\n`),
{ append: true },
);
Expand All @@ -68,10 +76,14 @@ And that's it! Our service is now complete and ready to be used.
```ts
import { startService } from "https://deno.land/x/telestion/mod.ts";
import { encode } from "https://deno.land/[email protected]/encoding/hex.ts";
import { resolve } from "https://deno.land/std/0.186.0/path/mod.ts";

const encoder = new TextEncoder();

const { messageBus } = await startService();
const { messageBus, dataDir } = await startService();

const logFilePath = resolve(dataDir, "log.txt");
await Deno.mkdir(dataDir, { recursive: true });

const logMessages = messageBus.subscribe("log.>");

Expand All @@ -83,7 +95,7 @@ for await (const msg of logMessages) {

console.log(`${currentTime} [${subject}] ${logMessage}`);
await Deno.writeFile(
"log.txt",
logFilePath,
encoder.encode(`${currentTime} [${subject}] ${logMessage}\n`),
{ append: true },
);
Expand Down

0 comments on commit 32b1333

Please sign in to comment.