How-To Guides
Flows Guides

Create, Write to, and Read from a Log File

5min

You can use Flows to create a log file on the Litmus Edge device, populate it, and read its contents.

Note: File names used in node configurations are case sensitive.

Create a Log File

Before any data can be written, you need to create create a file first.

To create a file in a flow:

  1. In Litmus Edge, navigate to the Flows Manager and create a new flow. See Create a Flow to learn more.
  2. Drag a write file node and an inject node to the canvas and wire them together.

    Flows canvas with write file node
    Flows canvas with write file node
    
  3. Double-click the write file node. The Edit write file node dialog box appears.
    • Filename: Enter a filename prefaced with a forward slash ( / ).
      • The forward slash is required in the filename. Example: /ftp-data/testlogfile.log
      • The file is created on the Litmus Edge device.
      • If a subdirectory already exists, you can supply the full path to the file. Example: /ftp-data/testlogfile or /logfiles/testlogfile.
    • Action: Select append to file from the drop-down menu and click Done.

      Edit write file node dialog box
      Edit write file node dialog box
      
  4. Click Deploy to save the flow.
  5. Click the button on the left side of the inject (timestamp) node to inject the current timestamp into the write file node, which creates the file locally if it does not already exist. In this example, the /ftp-data/testlogfile.log file is saved.

Read a File

You can use a Read file node to access a file that you have created or written to.

To read a file:

  1. From the Flows canvas, drag a read file node, inject node, and debug node to the canvas and wire them together.

    Flows canvas with read file node
    Flows canvas with read file node
    
  2. Double-click the read file node. The Edit read file node dialog box appears.
    • Filename: Enter the filename prefaced with a forward slash ( / ). A forward slash is required to be the first character of the filename. Example: /ftp-data/testlogfile.log
    • Output: From the drop-down menu, select the standard single utf8 string format.
    • Click Done.

      Edit read file node dialog box
      Edit read file node dialog box
      
  3. Click Deploy.
  4. Drag the Sidebar up from the bottom of the canvas and select the Debug icon.

    Debug icon on flows canvas
    Debug icon on flows canvas
    
  5. Click the inject node button to trigger the flow. The debug area shows the timestamp that has been written to the /ftp-data/testlogfile.log file.

    Output of reading a file
    Output of reading a file
    

Write to a Pre-Existing File

In the Create a Log File steps, the inject node triggered file creation. The following steps show how to write to an existing file.

To continue writing to the file that was created in Create a Log File:

  1. From the Flows canvas, double-click the write file node. The Edit write file node dialog box appears.
    • Filename: Enter a filename prefaced with a forward slash ( / ). We use the same file we created before: /ftp-data/testlogfile.log
    • Action: Select append to file from the drop-down menu.
    • Click Done.

      Edit write file node dialog box
      Edit write file node dialog box
      
  2. Click Deploy.
  3. Click the inject node button to inject the current timestamp into the write file node, which appends the output to the pre-existing file.
  4. Drag the Sidebar up from the bottom of the canvas and select the Debug icon.

    Debug icon on flows canvas
    Debug icon on flows canvas
    
  5. Click the inject node button from the read file node configuration to verify that the write task to the pre-existing file was successful.

    Output of reading a file
    Output of reading a file
    

Write a Message to a File

You can inject a string message into a pre-existing or a new file from the Flows.

To write a message (text string) to a file:

  1. In your Flow canvas, double-click the inject node. The Edit inject node dialog box appears.
    • msg.payload: From the drop-down menu, select String.
    • Enter text of your choice in the blank text field. In this example, we have entered: Hello World.
    • Click Done.

      Edit inject node dialog box
      Edit inject node dialog box
      
  2. Click Deploy.
  3. Click the inject node button to inject the message into the write file node, which appends the output to the pre-existing file.
  4. Drag the Sidebar up from the bottom of the canvas and select the Debug icon.

    Debug icon on flows canvas
    Debug icon on flows canvas
    
  5. Click the inject node button from the read file node configuration to verify that the message was written to the file.

    Output of reading a file
    Output of reading a file