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

Sunil Kumar Source

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")
  }

}
  • These logger information helps me a lot in understanding the problem and where my codebase failed with reason. So I cannot remove them because I need them during development.
  • When I am building the Jar file for my clients (production artifact), I don't need any logging.
  • If required I can use a pattern to identify logger lines by searching log.info as everywhere I am using the same variable name which is log.

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:

  • My project is a mixture of Java & Scala codebase and I want to delete all lines where log.info is found in any class be it Java or Scala in final jar.
  • I am using maven-assembly-plugin to create jar-with-dependencies as final product.
  • Any other solution is most welcomed.
javascalamavenloggingmaven-assembly-plugin

Answers

comments powered by Disqus