Create, Write to, and Read from a Log File
You can use Flows to create a log file on the Manufacturing Connect Edge device, populate it, and read its contents.
Note: File names used in node configurations are case sensitive.
Before any data can be written, you need to create create a file first.
To create a file in a flow:
- In Manufacturing Connect Edge, navigate to the Flows Manager and create a new flow. See Create a Flow to learn more.
Drag a write file node and an inject node to the canvas and wire them together.
- 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 Manufacturing Connect 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.
- Click Deploy to save the flow.
- 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.
You can use a Read file node to access a file that you have created or written to.
To read a file:
From the Flows canvas, drag a read file node, inject node, and debug node to the canvas and wire them together.
- 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.
- Click Deploy.
Drag the Sidebar up from the bottom of the canvas and select the Debug icon.
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.
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:
- 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.
- Click Deploy.
- Click the inject node button to inject the current timestamp into the write file node, which appends the output to the pre-existing file.
Drag the Sidebar up from the bottom of the canvas and select the Debug icon.
Click the inject node button from the read file node configuration to verify that the write task to the pre-existing file was successful.
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:
- 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.
- Click Deploy.
- Click the inject node button to inject the message into the write file node, which appends the output to the pre-existing file.
Drag the Sidebar up from the bottom of the canvas and select the Debug icon.
Click the inject node button from the read file node configuration to verify that the message was written to the file.