Add own log messages

Download Example

CODESYS includes a logger, where system components and fieldbusses are issuing status and error messages.

But this functionality is not limited to internal features. Every application and every library has access to the logger. You simply need to register a component, that you can identify yourself against the logger.

Component_Manager.CMAddComponent2('User Application', 16#00000001, ADR(udiCmpId), 0);

After that you can freely issue LogAdd to write new messages to the PLC log.

CmpLog.LogAdd2(CmpLog.LOG_STD_LOGGER, udiCmpId, CmpLog.LogClass.LOG_INFO, 1, 1, 'Test Message');

Posted by Ingo 2019-05-10
  • aliazzz

    aliazzz - 2019-05-18

    Nice posting! Thank you for this

     
  • mr337 - 2020-07-28

    Is the project file corrupted some how? Getting the following when trying to open in CodeSys 3.5 SP11

     
  • aliazzz

    aliazzz - 2020-07-29

    I think this example is for SP14 or SP15, you use SP11.

     
  • mr337 - 2020-07-30

    Dang, thanks for the info!

     
  • pcimatti78 - 2020-10-08

    hi,
    why <>
    IF udiCmpId <> 0 THEN
    Component_Manager.CMAddComponent2('User Application', 16#00000001, ADR(udiCmpId), 0);
    END_IF

    does this operation be executed only the first time?

    BR, Paolo

     
    • andi - 2022-01-28

      Hi, the correct statement should be
      IF udiCmpId = 0 THEN
      so that it will called once at the beginning

       
      • hermsen

        hermsen - 2022-01-28

        True, but it makes more sense that this code needs to be executed in implicitly available methods:

            FB_init
            FB_reinit
            FB_exit
        

        If those methods are used, the code will be robust and only called once per triggering event (Init, Re-Init or exit) and can be tailored accordingly.

         
  • sgronchi - 2021-08-06

    Do you know why DTClogger seems not to work if passed a custom logger with these options?
    DM1_logOptions : CmpLog.LogOptions :=
    (szName := 'J1939_DM1',
    bEnable := 1,
    uiType := CmpLog.LogTypes.LT_NORMAL OR CmpLog.LogTypes.LT_TIMESTAMP_RTC OR CmpLog.LogTypes.LT_NO_DISABLE OR CmpLog.LogTypes.LT_DUMP_ALWAYS, // LT_DUMP_ASYNC as in LT_STD results in the same
    uiFilter := CmpLog.LogClass.LOG_INFO OR CmpLog.LogClass.LOG_WARNING OR CmpLog.LogClass.LOG_ERROR OR CmpLog.LogClass.LOG_EXCEPTION,
    iMaxEntries := 1000,
    iMaxFileSize := 50000,
    iMaxFiles := 20);

    'J1939_DM1' log is always empty, but DTCs end up in the device-specific log (I think thanks to DM1 read...)

     

Log in to post a comment.