Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Logger

In this example we will create a logger inside our system that formats messages by adding to them the time and level.

Let us create an interface __logger__ with a simple method log:

cd ~/.local/share/irnix
mkdir __logger__
touch __logger__/.self
~/.local/share/irnix
├── .self
└── __logger__
    └── .self

.self:

log: stdin! level? stdout!

The method log reads a message from stdin, formats it, and outputs the formatted message.

  • stdin! adds a check so that stdin is not empty.
  • level? is an optional argument allowing one to specify the level.
  • stdout! means that the method must output something in any case.

Our next step will be adding a concrete implementation, which will be logm. To do this we create a directory logm in the namespace and add the method log:

cd ~/.local/share/irnix
mkdir logm
cd logm
ln /bin/logm log
cat ../__logger__/.self > .self
~/.local/share/irnix
├── __logger__
│   ├── .self
│   └── logm -> ../logm
└── logm
    ├── .self
    └── log -> /bin/logm

In this case the method log is a link to the binary file logm, which formats the message.

echo "message" | irnix e __logger__.log
Sun, 2 Nov 2025 19:58:33 +0000 INFO message

Now we can call the logger in other scripts (for example, in the bodies of other methods) and at the same time have the ability to replace the logger with another one, without changing the calls.

For instance, we can change the implementation of log to a simple Bash script:

#!/bin/bash

echo "$(date +%s): $(</dev/stdin)"