Built in logger to a different file

I would like to use the built in logger to log data into a custom file. Is this possible? Or do I need to just build my own logger? Could I just build an instance of LoggerEx and provide it a file path in some way?

I would use the wrapper logs, however I need make sure these log files only have the content I am intending to log and the wrapper logs have too much extra in this case.

You could theoretically do this with logback, via the logback.xml file. You’d basically just hardcode your custom logger name to go to a specific file appender:

3 Likes

Okay so I think this is definitely what I am looking for. However for some reason I am struggling to get it to work, want to make sure its not ignitions implementation of logback that is affecting what I did.

I updated my logback.xml to include the following:
Appender:

<appender name="CUSTOM-LOGGER-FILE" class="ch.qos.logback.core.FileAppender">
      <file>logs/custom-logger.log</file>
      <append>true</append>
      <encoder>
          <pattern>%.-1p [%-30c{1}] [%d{HH:mm:ss,SSS}]: %m %X%n</pattern>
      </encoder>
  </appender>

Logger:

 <logger name="custom-logger" level="TRACE" additivity="false">
      <appender-ref ref="CUSTOM-LOGGER-FILE"/>
  </logger>
Full logback.xml
<?xml version="1.0" encoding="UTF-8"?>

<!-- For assistance related to logback-translator or configuration  -->
<!-- files in general, please contact the logback user mailing list -->
<!-- at http://www.qos.ch/mailman/listinfo/logback-user             -->
<!--                                                                -->
<!-- For professional support please see                            -->
<!--    http://www.qos.ch/shop/products/professionalSupport         -->
<!--                                                                -->
<configuration debug="true">
  <appender name="SysoutAppender" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%.-1p [%-30c{1}] [%d{HH:mm:ss,SSS}]: %m %X%n</pattern>
    </encoder>
  </appender>
  <appender name="CUSTOM-LOGGER-FILE" class="ch.qos.logback.core.FileAppender">
      <file>logs/custom-logger.log</file>
      <append>true</append>
      <encoder>
          <pattern>%.-1p [%-30c{1}] [%d{HH:mm:ss,SSS}]: %m %X%n</pattern>
      </encoder>
  </appender>
  <appender name="DB" class="com.inductiveautomation.logging.SQLiteAppender">
    <dir>logs</dir>
    <!--
      Maintenance Settings
      entryLimit: The maximum number of entries in the database. However, at any given time, there may be more than this number, due to how cleanup works.
      maxEventsPerMaintenance: The number of event that can happen before a maintenance cycle occurs.
      minTimeBetweenMaintenance: The minimum time (max frequency) between maintenance events. Takes precedent over max events.
      vacuumFrequency: The number of maintenance cycles before a "vacuum" is performed, to recover disk space.

      On disk, most log events are between 600-800 bytes.
    <entryLimit>50000</entryLimit>
    <maxEventsPerMaintenance>5000</maxEventsPerMaintenance>
    <minTimeBetweenMaintenance>60000</minTimeBetweenMaintenance>
    <vacuumFrequency>3</vacuumFrequency>
    -->
  </appender>
  <appender name="SysoutAsync" class="ch.qos.logback.classic.AsyncAppender" queueSize="1000" discardingThreshold="0">
    <appender-ref ref="SysoutAppender" />
  </appender>
  <appender name="DBAsync" class="ch.qos.logback.classic.AsyncAppender" queueSize="100000" discardingThreshold="0">
    <appender-ref ref="DB" />
  </appender>
  <logger name="custom-logger" level="TRACE" additivity="false">
      <appender-ref ref="CUSTOM-LOGGER-FILE"/>
  </logger>
  <root level="INFO">
    <appender-ref ref="SysoutAsync"/>
    <appender-ref ref="DBAsync"/>
  </root>
</configuration>

And then I tried to run this in both the script console and a perspective button to test different scopes:

logger = system.util.getLogger("custom-logger")
logger.trace("trace")
logger.debug("debug")
logger.info("info")
logger.warn("warn")
logger.error("error")

However I only get the logs when I do it from the perspective button, not the script console.

T [custom-logger                 ] [17:40:52]: trace component=root/Button, view=Button@D, project-name=TEST
D [custom-logger                 ] [17:40:52]: debug component=root/Button, view=Button@D, project-name=TEST
I [custom-logger                 ] [17:40:52]: info component=root/Button, view=Button@D, project-name=TEST
W [custom-logger                 ] [17:40:52]: warn component=root/Button, view=Button@D, project-name=TEST
E [custom-logger                 ] [17:40:52]: error component=root/Button, view=Button@D, project-name=TEST

Any idea if there is something I have configured wrong that is not allowing it to work from the client scope as well?

Note: I did try using ch.qos.logback.core.ConsoleAppender instead of ch.qos.logback.core.FileAppender but then I couldn’t get anything to write to the file, so I used the FileAppender class mentioned in the above link.

logback.xml only controls logs on the gateway. When you execute something in the script console, that’s happening on your local JVM where the designer is running.
Perspective scripts are actually executing on the gateway, though, so you’ll end up with the right results.

If you want to automatically send logs from clients/designers to the gateway logs, you could do that (with a different pattern), but, y’know, be careful.

3 Likes

lol, I think I'll be good with gateway scoped logs :wink:

1 Like

Just a check for future safety, will an upgrade possible blow this away?

I don’t think so, I’m pretty sure the logback.xml should be preserved in your .gwbk.

1 Like

@PGriffith I was looking over this article. At the bottom, Steven mentions at the bottom that logback.xml is not contained in .gwbk. How To Migrate the Ignition Loggers using Logback.XML – Inductive Automation Help Center

Can anyone confirm that logback settings are in the .gwbk? If not can it be added in future release?

̶T̶h̶e̶y̶ ̶a̶r̶e̶ ̶i̶n̶ ̶i̶t̶,̶ ̶I̶ ̶h̶a̶v̶e̶ ̶b̶e̶e̶n̶ ̶u̶s̶i̶n̶g̶ ̶t̶h̶a̶t̶ ̶t̶o̶ ̶m̶o̶v̶e̶ ̶i̶t̶ ̶a̶r̶o̶u̶n̶d̶ ̶t̶o̶ ̶o̶t̶h̶e̶r̶ ̶d̶e̶v̶e̶l̶o̶p̶m̶e̶n̶t̶ ̶g̶a̶t̶e̶w̶a̶y̶s̶

EDIT: I was not thinking, listen to Paul :slightly_smiling_face:

I’m not sure they are (at least, I don’t see why they would be from the current state of the code) but I filed a ticket to ensure that they are, so either we’ll make necessary changes or someone else will prove me wrong :slight_smile:

2 Likes

Oh wait you’re right! I actually have left a copy in my projects folder since that’s the folder I manage with git and move it that way. My bad I forgot that we had git automatically handling that guy as well!

Ignition Release Notes for Version 8.1.17-rc1 | Inductive Automation

Starting in 8.1.17, looks like logback is now included in .gwbk file.

1 Like