Sunil Kumar
Sunil Kumar

Reputation: 632

Delete logger lines from final jar while performing maven build or package

I know that it is possible to delete or skip files and folders while doing maven clean.

My problem is that I want to delete some specific lines (log.info lines) from final jar while doing mvn clean package.

My full codebase is filled with logger lines similar to the following codebase:

class MyClass {

  def main(args: Array[String]) {
    val log = Utils.getLogger
    log.info("Application started")

    val url = "https://sunilkumar.in"
    log.info(s"Trying to fetch content from url: $url")

    ...

    log.info("Successfully fetched content. Doing something now.")

    val v1 = "variable1"
    val v2 = "variable2"

    log.info(s"Starting to do something with v1: $v1 and v2: $v2")

    try {

      ...

      log.info("Successfully done something with variables")
    }
    catch {
      case e: Exception =>
        log.severe(s"Failed to do something. Got exception with message: ${e.getMessage}")
    }

    log.info("Continuing the work")

    ...

    log.info("Finished everything")
  }

}

My question is:

I don't know where and how to use this pattern to delete lines from final jar while doing mvn clean build package so that:

  1. there should be no logging.
  2. If I open up Jar file and convert .class to .java file then there should be no logging codebase.

Note:

Upvotes: 0

Views: 274

Answers (1)

Vinoth A
Vinoth A

Reputation: 1239

Are you using slf4j, if so then consider setting the level to error. By setting it to error, none the log.info lines will get printed.

A sample config for reference

    <configuration scan="true" scanPeriod="60 seconds">

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSSZ,UTC} %X{requestId} %X{req.remoteHost} %X{req.xForwardedFor}
                %X{req.requestURI} [%thread] %-5level [%logger{0}] %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="error">
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>

Here, I am printing log.error to console, so info lines will not be printed. By changing the level to info, you could print both info and error statements

Upvotes: 1

Related Questions