Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback logger in inner class

I have outer public class ClassifierModule and inner public class ClassifierRunnable. Logger implemented in outer class works fine. But when I try to implement it in inner class it doesn't work at all. I mean, there is no error, but logback doesn't create logs.

How to implement logger in inner class? (I've learnt so far that it should be non-ststic).

private final static Logger logger = LoggerFactory.getLogger(ClassifierModule.class);

Here is my code:

//outer class
public class ClassifierModule extends ReactContextBaseJavaModule implements BufferListener {

    public ClassifierModule(ReactApplicationContext reactContext) {
        super(reactContext);
        appState = ((MainApplication)reactContext.getApplicationContext());
        }     

    @Override
    public String getName() {
      return "Classifier";
    }

    @ReactMethod
    public void saveLog() {
       logger.info("Hello world");
    }

    //inner class        
    public class ClassifierRunnable implements Runnable {

        public ClassifierRunnable(double[][] buffer) {
            rawBuffer = buffer;
            PSD = new double[NUM_CHANNELS][nbBins];
        }

        private final Logger logger = LoggerFactory.getLogger(ClassifierRunnable.class);

        @Override
        public void run() {
            if(isLogging) {

                int a = 11;
                int b = 24;

                logger.info(a)

            }
    }
}

My logback.xml is configured as below:

<configuration>
  <!-- Create a file appender for a log in the application's data directory -->
  <appender name="file" class="ch.qos.logback.core.FileAppender">
    <file>/data/data/com.eeg_project/files/log/eegdata.log</file>
    <encoder>
      <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- Write INFO (and higher-level) messages to the log file -->
  <root level="INFO">
    <appender-ref ref="file" />
  </root>
</configuration>
like image 751
Greynairod Avatar asked Aug 03 '26 11:08

Greynairod


1 Answers

If you want to add log level for inner classes, you need to add below configuration to the logback.xml file. it worked for me.

Since it is an inner class you need to separate it using $ sign from the parent class.

<logger name="packagename.ClassifierModule$ClassifierRunnable " level="INFO" additivity="false">
    <appender-ref ref="file" />
</logger>

Hope this helps you with your configuration. thanks

like image 64
Dilanka M Avatar answered Aug 06 '26 04:08

Dilanka M