Include Script Location

Q: How do I get the Ant script location in messages?

   The answer to this question depends the actual logging system you use. Log4Ant supports Mapped Diagnostic Contexts(MDC) and defines several standard MDC properties that describe the current Ant context for every logged event. If you use a logging system like Log4J, or Logback, or another SLF4J aware system that understands MDCs you can get this information easily by using the proper layout pattern to extract MDC elements for your logging system. The full set of Log4Ant supplied MDC properties includes the active project’s name, the enclosing target’s name, the source task’s name, and several pieces of script location information like the script file’s full path, the exact script line and column, etc.

The following snippet shows a Logback configuration pattern to display the current filename and line for events logged to the “TICKS” category.

<appender name="TICKS" class="ch.qos.logback.core.ConsoleAppender">
  <layout class="ch.qos.logback.classic.PatternLayout">
    <Pattern>%mdc{.location}: %m%n</Pattern>
  </layout>
  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>INFO</level>
  </filter>
</appender>

The output of a call to the Log4Ant <checkpoint> task might look something like:

deploy_cfbb-12.3.0.xml@28: [TICK] [G] [20001001T123530-0400]
deploy_cfbb-12.3.0.xml@67: [TICK] [A] [20001001T125231-0400]
...

Builtin MDC Properties

Below is the set of MDC properties Log4Ant automatically determines and installs as of Dec-2008. All Log4Ant emitting tasks (show, checkpoint, capturelogs, etc.) will define these properties. Note that all property names begin with a period (‘.’) and if Log4Ant is unable to determine a value for a particular property, that property’s value is set to the empty string.

Name Description Example
.task Logging task’s name “emit:checkpoint”
.project Logging task’s Ant project’s name “CFBB_NightlyBuild”
.target Logging task’s enclosing Ant target’s name “programmertests”
.location Ant script’s file and line short form “build-cfbb.xml@125”
.file Ant script’s file name only “build-cfbb.xml”
.line Ant script’s line “125”
.column Ant script’s line column “24”
.longlocation Ant script’s location full form “/staging/builds/cfbb/12.3.0/build-cfbb.xml@125:24”
.longfile Ant script’s file path full form “/staging/builds/cfbb/12.3.0/build-cfbb.xml”
.noiselevel Original Log4Ant noise level (unmapped) “fatal”

Related Tips


Navigation
Personal Tools