[JAVA] I want to hook log file generation / open with log4j # FileAppender

I wonder if there is demand (´ ・ ω ・ `) If you want to hook the timing when a log file is generated or opened in FileAppender, the class that inherits FileAppender and overrides setFile is changed to log4j.Appender. Specify.

For example, if you want to switch the log file for each date, more specifically, sample.log.20180211 for the log of February 11, 2018, and sample. For the log of February 12, 2018. If you want to record in log.20180212, prepare the following original file and log4j.properties.

log4j.properties


log4j.logger.sample=DEBUG, SAMPLE
log4j.appender.SAMPLE=sample.logger.SampleFileAppender
log4j.appender.SAMPLE.File=C:\\logging\\sample.log
log4j.appender.SAMPLE.Append=true
log4j.appender.SAMPLE.layout=org.apache.log4j.PatternLayout
log4j.appender.SAMPLE.layout.ConversionPattern=%d %p %m %n

SampleFileAppender.java


package sample.logger;

import org.apache.log4j.FileAppender;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class SampleFileAppender extends FileAppender {

    @Override
    synchronized public void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) throws IOException {
        Path path = Paths.get(fileName);
        String baseName = path.getFileName() + "." + DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now());
        super.setFile(Paths.get(path.getParent().toString(), baseName).toString(), append, bufferedIO, bufferSize);
    }

}

If you think about it carefully, DailyRollingFileAppender is enough for this purpose (´ ・ ω ・ `) I'm sorry it's not a good sample (´ ・ ω ・ `)

Recommended Posts

I want to hook log file generation / open with log4j # FileAppender
I want to monitor a specific file with WatchService
[Eclipse] I want to open the same file twice [Split editor]
I just wanted to logrotate with log4j 2
I want to use DBViewer with Eclipse 2018-12! !!
I want to test Action Cable with RSpec test
I want to use java8 forEach with index
I want to play with Firestore from Rails
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to be able to read a file using refile with administrate [rails6]
I tried to implement file upload with Spring MVC
I want to embed any TraceId in the log
Output log to external file with slf4j + logback with Maven
I want to dark mode with the SWT app
I want to authenticate users to Rails with Devise + OmniAuth
I want to transition screens with kotlin and java!
I want to get along with Map [Java beginner]
I want to redirect sound from Ubuntu with xrdp
I want to simplify the log output on Android
I want to push an app made with Rails 6 to GitHub
I want to change the log output settings of UtilLoggingJdbcLogger
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
I want to manually send an authorization email with Devise
I want to distinct the duplicated data with has_many through
I want to implement various functions with kotlin and java!
I want to pass the startup command to postgres with docker-compose.
[Java] I want to test standard input & standard output with JUnit
I want to convert characters ...
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
I want to make a button with a line break with link_to [Note]
I want to connect SONY headphones WH-1000XM4 with LDAC on ubuntu 20.04! !!
I want to add a browsing function with ruby on rails
I want to return to the previous screen with kotlin and java!
I want to INSERT Spring Local Time with MySQL Time (also milliseconds)
I want to avoid OutOfMemory when outputting large files with POI
I want to operate cron with GUI, so I will install Dkron
Try to make a cross-platform application with JRuby (jar file generation)
How to open a script file from Ubuntu with VS code
[Java] I want to perform distinct with the key in the object
[Development log ⑮] I want to add an external link or PDF link
[Rails] I want to add data to Params when transitioning with link_to
I want to perform asynchronous processing and periodic execution with Rail !!!
I want to extract between character strings with a regular expression
I want to create a Servlet war file with OpenJDK on CentOS7. Without mvn. With no internet connection.
I want to download a file on the Internet using Ruby and save it locally (with caution)
Swift: I want to chain arrays
I want to use FormObject well
Minimal Rails with reduced file generation
I want to convert InputStream to String
I tried to interact with Java
I want to docker-compose up Next.js!
Log output to file in Java
Enable log output to both file and console using log4j in Eclipse.
I want to display images with REST Controller of Java and Spring!
I made an app to scribble with PencilKit on a PDF file
I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)