Skip to content

End to end example: Log Service

Note

The author generated this text in part with GPT-3, OpenAI’s large-scale language-generation model. Upon generating draft language, the author reviewed, edited, and revised the language to their own liking and takes ultimate responsibility for the content of this publication.

This tutorial will explain step-by-step how to write a log service that will listen for messages on the log.> subject and write them to a file.

Steps

  1. First, we need to import the startService function from our library (lib.ts) and the encode function from the standard Deno library.

    import { startService } from "https://deno.land/x/telestion/mod.ts";
    import { encode } from "https://deno.land/std@0.186.0/encoding/hex.ts";
    import { resolve } from "https://deno.land/std@0.186.0/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.

    const encoder = new TextEncoder();
    
  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.

    const { messageBus, dataDir } = await startService();
    
  4. We then resolve the log file path and create its parent directory if it doesn’t exist yet.

    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..

    const logMessages = messageBus.subscribe("log.>");
    
  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.

    for await (const msg of logMessages) {
      try {
        const currentTime = new Date().toISOString();
        const logMessage = encode(msg.data).toString();
        const subject = msg.subject.split(".")[1];
    
  7. We log the message to the console and write it to a file (appending it to the end).

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

And that’s it! Our service is now complete and ready to be used.

Final Code

import { startService } from "https://deno.land/x/telestion/mod.ts";
import { encode } from "https://deno.land/std@0.186.0/encoding/hex.ts";
import { resolve } from "https://deno.land/std/0.186.0/path/mod.ts";

const encoder = new TextEncoder();

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

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

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

for await (const msg of logMessages) {
  try {
    const currentTime = new Date().toISOString();
    const logMessage = encode(msg.data).toString();
    const subject = msg.subject.split(".")[1];

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