Logger basic questions scheduled event gateway script

image

I think I just need to put in whatever I want in the string area where I have message, and the logger is that simple?
When this script fires, I will have a debug level entry on the gateway logs?

I never had to troubleshoot something this way before, but it was requested.
So I am probably not aware of different configuration options.

I put the name of the project and the name of the script the logger is in for both the logger name and string, might be overkill. Just hate when I have a logger I can't find.

If you fix your mixed case, yes.

Note that loggers do not process debug level message by default. You must deliberate set the level for that logger to DEBUG in the gateway web interface.

Also note that there are methods like .debugf(), .tracef(), .infof(), et cetera, that perform printf-style string formatting for you. This is especially helpful when the string you wish to log is complex, and would consume a lot of CPU time unnecessarily when the log level would skip it. Use them wherever they can save CPU time.

Consider also moving your event code to a project library script function, and defining your logger variable outside any function. The one instance can safely and efficiently be shared by all of the functions in the library script.

1 Like

Do you mean that my script would be in a project library, and have my scheduled event call a function in a project library script?
I am not sure I understand.

The manual says:
"To change the logging level for a Logger in a Gateway script, go to Status > Diagnostics > Logs > Click the Settings settings button icon icon."

How does the logger I setup get populated to the settings?

I named my logger like:
logger = system.util.getLogger("projectNameGatewayScriptPurpose")
My string is the same.

Can you help me understand or point me to where to read to understand how the loggers are instanced and how they populate in settings?
I did not find this logger in there to set to debug.
I thought that by choosing logger.debug.... this would set the level.

Yes. Making all events delegate to project library script functions with a one-liner provides opportunities to optimize performance with share imports and long-lived variables, makes it easy to group related functionality, and places the content in the gateway filesystem as plain text for source code control friendliness. For certain kinds of gateway events, it also avoids some legacy scope gotchas. At least until v8.3 arrives.

The first time something actually logs to it.

1 Like

It sets the level of that message. Setting the level of the logger controls what level of messages to process (versus discarding).

1 Like

Thank you again and again

I think I did something wrong.

It is only two lines of code, but I don't see the log happening when the script ran.
I can check the script ran successfully in 7ms on the gateway.

I can find the logger in the logs settings on the gateway, and have changed the level from info to debug level.

Did the script run after you changed the level from info to debug?

1 Like

Not yet.
I just changed the level today.
The script ran twice so far, and the level was info before.

Yet, when I go to logs, or check for loggers in the gateway scripts area of the gateway, I don't see any loggers listed there.

My two lines of code for the logger are the creation of the logger, and where I set the message.

Can you post your code just so we can check it for sanity?

And what do you mean by:

Can you post a screen shot of this?

Would only be on the info side, but just for clarity, both.

You won't see anything there from your own loggers. Look in the main status logs page. Log messages sent at DEBUG level while the logger level was INFO were discarded, so of course there's nothing to see yet.

The "Min Level" setting is not sufficient--you also need to click the gear icon, find your logger, and set the specific logger level to DEBUG.

1 Like

That makes sense, thanks.

Edit
I see the min level setting on the logs that you mean.

Edit: I don't know where the min level setting is.
I went to the logs, the gear, and set for debug in the dropdown.
image

I used logger.debug( for the message. I think you mean this is the min level setting, but I am not sure.

Let's break things down to try and make it clearer:

  • When you use a logger in a script, you call one of its methods that will log a message at a certain level. Here you called .debug(), so a debug message was sent to the logging engine
  • For each logger, the logging engine as a configured level. When it receives a message for a logger, it compares the message's level to the configured level on the logger. If the message's level is equal or above the configured minimum level, it logs it. Otherwise, it is discarded.
    In your case, the logger was configured to register messages of level info or higher, so your debug message was discarded.
  • When displaying logs, you can filter on levels. But if the message was discarded because its level was too low when it was emitted, it is lost. It will not show up there however you configure or filter anything now.
    This is why you can't find your message in the logs.

So we have several layers of configuration to take into account.
Configure your logger on the gateway page, then log a message with a level that's at least as high as the configuration, and go back to check if it's there. It should be.

2 Likes

Just making sure this wasn't missed:

This is referring to your original script:

logger = system.util.getLogger("demo")
Logger.debug('message')

logger != Logger (line 3 should be logger.debug("message")).

Side note, watch your quote consistency (single vs double). Broadly won't hurt anything, but best to be consistent throughout your scripts.

1 Like