# Logging scripts

**URL:** <https://forum.inductiveautomation.com/t/logging-scripts/7448>\
**Category:** Ignition\
**Created:** [October 22, 2013, 3:36pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448 "2013-10-22T15:36:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Bridgeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bridgeman/32/3610_2.png) [@Dan\_Bridgeman](https://forum.inductiveautomation.com/u/Dan_Bridgeman)\
**Post date:** [October 22, 2013, 3:36pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/1 "2013-10-22T15:36:39Z")

</div>

I found [this old thread](http://www.inductiveautomation.com/forum/viewtopic.php?f=70&t=7185&p=21414) in the forum that shows a way to log Gateway scripts by importing Apache’s log4j, and thought I’d give it a try. But as far as I can tell, it’s not working.

I used the code shown in that thread, and left the logger named “Test”, since I’m just playing with it at this point

[code]#Import Apache log4j  
import sys  
sys.add\_package(‘org.apache.log4j’)  
from org.apache.log4j import LogManager

#Create the logger with the name you want  
logger = LogManager.getLogger(“Test”)

#Log the message  
logger.info(“Message”)[/code]

---

<div class="post-metadata">

**Author:** ![Dan\_Bridgeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bridgeman/32/3610_2.png) [@Dan\_Bridgeman](https://forum.inductiveautomation.com/u/Dan_Bridgeman)\
**Post date:** [October 22, 2013, 3:41pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/2 "2013-10-22T15:41:46Z")

</div>

…and I hit submit instead of preview…

Anyway, I have called that directly in a Gateway Timer script, and it didn’t log anything. I have also tried making a script module that imports the apache module and sets up the logger, and that didn’t work from a Gateway script nor from the script playground.

Should I be using one of the existing Loggers from the System Console Levels page? If I can actually create one and call it whatever I want, should that Logger start showing up in this page so I can set a Level for it.

Also, should this work for scripts on Client screens as well from the Gateway?

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [October 22, 2013, 6:21pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/3 "2013-10-22T18:21:50Z")

</div>

To get an instance of a logger, use system.util.logger(“LoggerName”).loglevel where loglevel is one of the following:  
info  
warn  
error  
debug  
trace  
fatal

This can be used in any context.

---

<div class="post-metadata">

**Author:** ![Dan\_Bridgeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bridgeman/32/3610_2.png) [@Dan\_Bridgeman](https://forum.inductiveautomation.com/u/Dan_Bridgeman)\
**Post date:** [October 22, 2013, 7:34pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/4 "2013-10-22T19:34:47Z")

</div>

Ah, I’ve never seen that system.util.logger function. Is it documented anywhere? I can’t figure out what to do with that instance. It doesn’t seem to have much functionality attached to it:

[code]import system

logger = system.util.logger(“Alarming”).info  
print dir(logger. **class** )[/code]

returns: [‘ **call** ’, ‘ **class** ’, ‘ **cmp** ’, ‘ **delattr** ’, ‘ **doc** ’, ‘ **get** ’, ‘ **getattribute** ’, ‘ **hash** ’, ‘ **init** ’, ‘ **new** ’, ‘ **reduce** ’, ‘ **reduce\_ex** ’, ‘ **repr** ’, ‘ **setattr** ’, ‘ **str** ’, ‘im\_class’, ‘im\_func’, ‘im\_self’]

Also, can I use this to create a new Logger, or do I have to use an existing one? And if I have to use an existing one, are there established best practices for which of them to use?

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [October 22, 2013, 7:38pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/5 "2013-10-22T19:38:55Z")

</div>

That call returns a log4j logger, similar to the code you posted above.

You would do something like

```auto
system.util.logger("Test Logger").info("Stuff to log")
```

or

`logger = system.util.logger("Test Logger")
logger.info("Info to log")
logger.error("Error to log")`

---

<div class="post-metadata">

**Author:** ![Dan\_Bridgeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bridgeman/32/3610_2.png) [@Dan\_Bridgeman](https://forum.inductiveautomation.com/u/Dan_Bridgeman)\
**Post date:** [October 23, 2013, 2:05pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/6 "2013-10-23T14:05:54Z")

</div>

I tried running that exact code (with an import system) in the script playground. It did cause some things to print out to my output console, but nothing got written to the wrapper.log, nor did I see anything on the system console.

Am I missing a step here? ![:scratch:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/76d1620bec252bfac07050c889ff5a91dc8a9c25.gif ":scratch:")

---

<div class="post-metadata">

**Author:** ![adamaustin](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@adamaustin](https://forum.inductiveautomation.com/u/adamaustin)\
**Post date:** [October 23, 2013, 2:43pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/7 "2013-10-23T14:43:39Z")

</div>

I was only able to get the code to work in the Gateway context. In other words, it looks like it has to run in a Gateway Timer script, for example. This is true for either the system.util.logger code or the original code that was posted. Both work.

---

<div class="post-metadata">

**Author:** ![Dan\_Bridgeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bridgeman/32/3610_2.png) [@Dan\_Bridgeman](https://forum.inductiveautomation.com/u/Dan_Bridgeman)\
**Post date:** [October 23, 2013, 6:06pm UTC](https://forum.inductiveautomation.com/t/logging-scripts/7448/8 "2013-10-23T18:06:54Z")

</div>

Okay. I’m actually working out some other Gateway Timer script issues right now, so once that’s sussed out I’ll try some dummy logging.

Thanks for the tip!
