Sensei Logo Sensei Hub

Recipe - SLF4J Logging: Replace java.util.logging with SLF4J (Log Exception Trace)

Description:
Standardise on SLF4J by replacing this call
Level:
marked_information
Language:
  • java
Tags:
  • SLF4J
  • framework specific
  • logging
  • quality
Recipe
id: scw:logging:slf4j-jul-exception-trace
version: 10
metadata:
  name: 'SLF4J Logging: Replace java.util.logging with SLF4J (Log Exception Trace)'
  shortDescription: Standardise on SLF4J by replacing this call
  level: marked_information
  language: java
  scwCategory: insufficient_logging:generic
  cweCategory: 778
  enabled: true
  tags: SLF4J;framework specific;logging;quality
search:
  methodcall:
    args:
      1:
        referenceTo:
          name:
            matches: java\.util\.logging\.Level\.(FINER|FINEST)
      2:
        type: java.lang.Throwable
    in:
      typeDeclaration:
        anyOf:
        - with:
            child:
              field:
                name: logger
                type: org.slf4j.Logger
        - without:
            child:
              field:
                name: logger
                type:
                  reference:
                    not: org.slf4j.Logger
                  checkInheritance: true
    argCount: 3
    name: log
    type: java.util.logging.Logger
availableFixes:
- name: '[OPTIONAL] Replace with SLF4J debug function'
  actions:
  - rewrite:
      to: logger.debug("", {{{arguments.1}}})
      target: self
  - addField:
      field: private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger({{{ containingClass.containingClass.name }}}.class);
      target: parentClass
- name: '[OPTIONAL] Replace with SLF4J error function'
  actions:
  - rewrite:
      to: logger.error("", {{{arguments.1}}})
      target: self
  - addField:
      field: private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger({{{ containingClass.containingClass.name }}}.class);
      target: parentClass
- name: '[OPTIONAL] Replace with SLF4J info function'
  actions:
  - rewrite:
      to: logger.info("", {{{arguments.1}}})
      target: self
  - addField:
      field: private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger({{{ containingClass.containingClass.name }}}.class);
      target: parentClass
- name: '[OPTIONAL] Replace with SLF4J warn function'
  actions:
  - rewrite:
      to: logger.warn("", {{{arguments.1}}})
      target: self
  - addField:
      field: private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger({{{ containingClass.containingClass.name }}}.class);
      target: parentClass
- name: ' [RECOMMENDED] Replace with SLF4J trace function'
  actions:
  - rewrite:
      to: logger.trace("", {{{arguments.1}}})
      target: self
  - addField:
      field: private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger({{{ containingClass.containingClass.name }}}.class);
      target: parentClass