Forum Discussion

krisdames's avatar
krisdames
Icon for Cirrus rankCirrus
Apr 19, 2013

Conditional logging

Hi,

 

I typically put this at the top of all my irules (learned it from the F5 consultant that set up our BigIP). This allows me to easily turn debugging on and off:

 

 

when RULE_INIT {

 

Set to 0 to disable debug logging

 

Set to 1 to enable logging to /var/log/ltm

 

set static::debug 1

 

}

 

 

Throughout my irule, I use logging statements like this (Example A):

 

 

 

if { $static::debug } { log local0. "debugging message" }

 

 

I'd like to create a block of logging statements like so (Example B):

 

 

if { $static::debug } {

 

log local0. "debugging message 1"

 

log local0. "debugging message 2"

 

}

 

 

But this does not seem to work. Both debug messages get logged, regardless of the value of static::debug. What am I doing wrong?

 

 

--

 

Kris

 

 

 

 

6 Replies

  • The static namespace is global to the BOX, so if you're using the static::debug variable in all of your iRules, it'll take the value (globally) of the last updated iRule.
  • Oh...well that makes perfect sense then. I also just found this which explains that: https://devcentral.f5.com/wiki/iRules.static.ashx

     

     

    Thanks for the timely reply. Time to change my iRules! :)

     

     

    --

     

    Kris

     

  • I also just learned this interesting tidbit:

     

     

    The only time this becomes even slightly confusing is when setting variables in the RULE_INIT event. All variables set under this event are treated as global, so keep that in mind. The same commands may be used against either local or global variables, though, so keep an eye out.

     

     

    Source: https://devcentral.f5.com/tech-tips/articles/irules-101-03-variables

     

     

    So not only should I not use the static namespace but I need a unique variable name if I want to initialize it in the RULE_INIT event, because even if I try to use a local variable it will actually be a global variable!

     

     

    --

     

    Kris

     

  • True statement about RULE_INIT variables. The biggest difference is how the variables are stored and accessed. The static namespace is specifically designed for CMP-compatibility - meaning the variables will be accessible across TMM instances. A non-static global variable is not only not CMP-capable, but will likely demote the entire virtual server to a single TMM (not good for performance). So static global variables can be a good thing, if used properly.

     

     

    By the way, while they are intended to be read-only once set, you can change a static global variable outside of RULE_INIT, but doing so will break CMP for that variable.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Kevin is, as usual, spot on about how RULE_INIT works and how to deal with variables being named and set there. There are also some mutterings internally about possibly looking at how this works and changing it in future versions. If you've got feedback on how this would work for you, now is definitely the time to let us know...don't be shy. ;)

     

     

    Thoughts?

     

    Colin
  • Kris, just in case if you have not yet seen this article. it is pretty handy.

     

     

    iRules logging to multiple locations with ease by Dan Holland

     

    https://devcentral.f5.com/tech-tips/articles/irules-logging-to-multiple-locations-with-ease