How to customize log format in automate.log?


#1

I would like to update the log preamble to include a custom attribute in addition to what is included by default. (The attribute would have been set via $evm.set_state_var).
a) How do I do this without updating the Rails application?
b) Or, if there is no way to do this without updating the Rails application, what would be the recommended way to do this? (Would I override the log method in MiqAeService, for instance, or is there a better way?)


#2

Is it ok, if it only affects the log lines you write yourself from your Automate Methods? Does is have to be in the real preamble, or is a prefix for the message ok too?

My first Idea would be to create a custom custom_log() method and include that with Embedded Methods into every Automate Method.

def custom_log(level, message)
  pre = $evm.state_var_exists?(:log_preamble) ? $evm.get_state?var(:log_preamble) : "No Preamble: "
  $evm.log(level, pre + message)
end

It is not exactly what you asked for, but it is probably the simplest way to achieve something close.

Although I have not messed with the logging myself. Perhaps there is some way to overwrite some of the settings for the logging library.


#3

The requirement is to include a specific custom attribute with every log statement, so either the preamble or a message prefix should work.

As it turns out, we’re currently using CloudForms 4.5, so we don’t have the ability to use Embedded Methods (at least based on my understanding). Would you happen to know if there’s any way to achieve a similar thing without Embedded Methods?

Also, the fix would have to work with legacy Automate code, so ideally I’d like to be able to override the existing log method so we don’t have to make modifications throughout the existing codebase.

Any suggestions? Thanks in advance!


#4

Embedded Methods is a new Feature in CloudForms 4.6. Prior to 4.6 many people build their own custom ruby-gem and installed that on the appliance.
From a technical point of view, this is trivial to do, depending on you organizational sturcture, this might be a problem.

$evm actually allows you to pass it a logger implementation. If you implment your own custom logger, you should be able to pass it to $evm with $evm.logger = MyCustomLogger.new()
Note, that this is still a setting specific to the current workspace,. therefore you would have to configure that once per request.

As I said, I have never tampered with the logging myself. I am guessing, from what I have seen, messing on the rails console an browsing github. (Also I am not a ManageIQ developer, therefore my knowledge of the internal logging structure is pretty limited and I have no idea if this kind of customization would be supported)


#5

Thanks for the update! Not sure if the custom gem approach would be feasible for us but will look into it. An ideal solution would be a centralized approach, one that allows us to override the logger’s behavior in one place (or at worst in only a few places), without having to make changes everywhere the logger is called.