Project-Wide Logger

I have a project script library that I would like to use one common logger for, but I can't seem to get the syntax correct.

I think the issue is that I need to be able to define the logger before calling system.util.getLogger() so that it's available at the global level.

I tried

logger = system.util.getLogger('Navigation')

But of course that didn't work because it was outside of a function, so it's not called and the logger is never created.

Then I tried.

logger
def startLogger():
	Navigation.logger = system.util.getLogger('Navigation')

and calling Navigation.startLogger() on client startup, but I get a 'logger not defined' error.

Is this possible to do so that I can have multiple scripts log to the same logger without using system.util.getLogger() in every function?

Putting it at the top of the script library file should definitely work, it runs once on scripting restart (basically whenever you save a script anywhere) and would define it then.

It's how I do all my logging in library scripts:

logger = system.util.getLogger('FCRM.Project.Library')

#rest of file
1 Like

You don’t need a common instance, just getting the logger by the same name is enough.

1 Like

Yes, but I'd rather just call logger = system.util.getLogger('Navigation') once and then use Navigation.logger('text') throughout.

1 Like

I hate retyping the name (and it's error-prone for my clumsy fingers) so I like to define it once.

2 Likes

When I do it this way I get a 'com.inductiveautomation.ignition.common.util.LoggerEx' object is not callable error

Do you specify file.logger or just use logger? - same error either way

Because that’s just the logger instance. The log methods are info, debug, warn, etc…

2 Likes

:man_facepalming:

Good catch, Kevin, I actually didn't notice that usage in the earlier post.
Also, you don't need to use foo.logger as the namespace is already implied to be the same library script you're referring to it from.

I tend to put this at the top of my scripts:

logger = system.util.getLogger(system.util.getProjectName() + '.' + __name__)
4 Likes

I think he wants to be able to call it anywhere, not just the script where it was defined.

1 Like

Both, actually.

It can be called from within the library where it is defined using logger.info() and outside of that library using Library.logger.info(). Technically, it can also be called within the library using Library.logger.info(), it's just redundant.

Ewww! Then you struggle to find the source of a particular log entry.

1 Like

You could, yes but the only other place I'm using it is on the docked navigation page.