Logging in TwinCAT with the on-board means is limited to the output as ADS event. The TcLog library presented here enables flexible logging to the file system.
It's usage is as simple as this:
Configure the core logger in your project:
VAR
_coreLogger : TcLogLib.TcLogCore(bufferSize := 100 * (Tc2_System.MAX_STRING_LENGTH + TcLogLib.Constants.FifoOverhead));
END_VAR
_coreLogger
.WriteToAds()
.WriteToFile('c:\logs\', 'sensor_data.txt')
.MinimumLevel(TcLogLib.LogLevels.Debug)
.RunLogger();
Then, maybe in a different POU, use TcLog
to log messages:
VAR
_logger: TcLogLib.TcLog;
_myInt : INT := 10;
_myVarInfo : __SYSTEM.VAR_INFO := __VARINFO(_myInt);
END_VAR
_logger
.AppendString('Let´s log some values: ')
.AppendAny(_myInt)
.AppendString(' - or some symbols: ')
.AppendVariable(_myVarInfo, _myInt)
.Error('');
This will log both messages to both the ADS output and the file system.
🚀 Features 🚀
🧪 Tests 🧪
The project contains both unit (TcUnit) and integration tests (xUnit).
See the installation guide for instructions on how to install TcLog.
Get quickly up and running with TcLog: Get Started
Find the full API reference here.
The library is licensed under the MIT License.
Contributions are welcome. Please see the contribution guide for details.
With log4TC there is another logging option for TwinCAT. This enables structured logging, but an additional Windows service must be installed, which communicates with the PLC library. TcLog
on the other hand comes as a pure PLC library.
The code for log4TC has been published as open source on GitHub.
This project is not affiliated with Beckhoff Automation GmbH & Co. KG and was first published in 2021 at my blog.