Debugging, Logger & Conditional Compilation

Aside from sharing knowledge, I sometimes blog to document and record something so I can use it again. This is one of those cases.

I use logger extensively, I would say on just about all my code. Code instrumentation is a critical aspect of writing good, maintainable, and scalable code.
However, sometimes, there’s is such a thing as too much logging or as I like to think about it, too verbose.

So I will often use PL/SQL Conditional Compilation to conditionally enable comments.

If I want the extra output, I will use this command before compiling a package:


Then the package will look like this:

package body intensive_data_load
gc_scope_prefix constant VARCHAR2(31) := lower($$PLSQL_UNIT) || '.';
procedure add_message(p_message_text in varchar2)
  l_scope  logger_logs.scope%type := gc_scope_prefix || 'add_message';
  l_params logger.tab_param;
  -- logger.append_param(l_params, 'p_param1', p_param1);
  logger.log('BEGIN', l_scope, null, l_params);

  mm_api.add_message(p_msg => g_msg, p_message_text => p_message_text);

  logger.log('.. ' || p_message_text, l_scope);
  logger.log('END', l_scope);

    when OTHERS then
      logger.log_error('Unhandled Exception', l_scope, null, l_params);
end add_message;

end intensive_data_load;

The flag VERBOSE_OUTPUT does not have any special meaning, I made it up. You can make up your own. But the flag could be something predefined like the Oracle DB version (look at dbms_db_version) or APEX version (looks at wwv_flow_api). See some more interesting examples here.

Anyway, I would rarely use the alter session command when I deploy the code to production. I don’t want the extra output all the time. However, if we ever need to troubleshoot we can re-compile the code with a new flag value.

You can turn the flag for your session and compile the package, like this:

alter package intensive_data_load compile;

Or with a single command:

alter package intensive_data_load compile
reuse settings;

Finally, don’t forget to turn the verbose output back off. ;-)

Hi, I'm Jorge Rimblas. Father, husband, photographer, Oraclenerd, Oracle APEX expert, Oracle ACE, coffee lover, car guy, gadget addict, etc... I'm a Senior APEX Consultant with Insum Solutions, a consulting firm specialized in Oracle databases and the APEX development tool. I have worked with Oracle since 1995 and done eBusiness Suite implementations and customizations. Nowadays I specialize almost exclusively in Oracle APEX.

Tagged with:

I love comments, write me a line