Home Java javaTutorial How to write logback configuration using java code

How to write logback configuration using java code

Jul 19, 2018 am 10:19 AM
java code

Provide others with a jar package, which can print logs directly and collect logs to a fixed location. In most cases now, the logback.xml file is used. If this file is still used, it may cause conflicts (of course you can use other name), in order to avoid having to load files, write the configuration directly in Java, as follows:

public class SecurityLoggerFactory {

    private static final String logPath = "/export/Logs/securityLog/securityLog.log";
    private static final String logPathHistory = "/export/Logs/securityLog/securityLog.log.%d";
    private static Logger logger = null;

    public static Logger getLogger(){
        return logger;
    }

    /**
     * 构造Logger
     * 设置路径
     * 设置滚动方式
     */
    static {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        //loggerContext.reset();
        RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
        rollingFileAppender.setContext(loggerContext);
        rollingFileAppender.setAppend(true);
        rollingFileAppender.setName("jdSecurityLogAppender");
        rollingFileAppender.setFile(logPath);

        TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy<>();
        rollingPolicy.setFileNamePattern(logPathHistory);
        rollingPolicy.setMaxHistory(10);
        rollingPolicy.setContext(loggerContext);
        rollingPolicy.setParent(rollingFileAppender);
        rollingPolicy.start();
        rollingFileAppender.setRollingPolicy(rollingPolicy);

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setPattern("%msg%n");
        encoder.setCharset(Charset.forName("UTF-8"));
        encoder.setContext(loggerContext);
        encoder.start();
        rollingFileAppender.setEncoder(encoder);

        rollingFileAppender.start();

        ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger("securityLogLogger");
        rootLogger.setLevel(Level.INFO);
        rootLogger.addAppender(rollingFileAppender);
        logger = rootLogger;
    }

    public static void main(String[] args) {
        SecurityLoggerFactory.getLogger().info("this is text log.");
    }
}
Copy after login

The client can print the relevant logs by directly calling SecurityLoggerFactory.getLogger().info(). In addition to outputting the log on the console, the log will also be output to the specified file.

Note:

The file path is the Linux path. If the file cannot be generated when executed in Windows, the Windows path must be used, such as: c:/securitylog/securitylog.log

The above is the detailed content of How to write logback configuration using java code. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Java to write code to implement love animation Use Java to write code to implement love animation Dec 23, 2023 pm 12:09 PM

Realizing love animation effects through Java code In the field of programming, animation effects are very common and popular. Various animation effects can be achieved through Java code, one of which is the heart animation effect. This article will introduce how to use Java code to achieve this effect and give specific code examples. The key to realizing the heart animation effect is to draw the heart-shaped pattern and achieve the animation effect by changing the position and color of the heart shape. Here is the code for a simple example: importjavax.swing.

How to call Amap API through Java code to implement path distance calculation How to call Amap API through Java code to implement path distance calculation Jul 29, 2023 pm 01:17 PM

How to call the Amap API through Java code to implement path distance calculation. As people's demand for real-time traffic conditions and navigation increases, map route planning becomes more and more important. As the leading map service provider in China, Amap's route planning function is favored by the majority of developers. This article will introduce how to call the Amap API through Java code to implement path distance calculation. Amap API provides a series of rich interfaces, including geocoding, reverse geocoding, route planning and other functions. In this article, we will focus on

How to call Amap API through Java code to implement path planning function How to call Amap API through Java code to implement path planning function Jul 30, 2023 pm 07:33 PM

How to call the Amap API through Java code to implement the path planning function. Map navigation has become one of the essential functions in our daily lives. In modern mobile applications, many applications have integrated route planning functions to help users easily find the optimal driving, walking or public transportation route. Amap API provides rich interfaces and functions to facilitate developers to easily implement map navigation functions. This article will introduce how to call the Amap API through Java code to implement the route planning function. First, we need to register Amap

Interpretation of Amap API documentation: Java code to implement real-time bus arrival information query Interpretation of Amap API documentation: Java code to implement real-time bus arrival information query Jul 31, 2023 pm 12:30 PM

Interpretation of Amap API documentation: Java code implements real-time bus arrival information query. With the popularity of smartphones and the development of urban transportation, bus travel has become an indispensable part of modern urban life. As a leading map service provider in China, Amap provides very powerful bus information functions, which can enable real-time query of bus arrival information. This article will introduce how to implement this function by interpreting the Amap API documentation and providing Java code examples. First, we need to understand the Amap API

Java code example: Using Alibaba Cloud DTS interface to achieve database synchronization Java code example: Using Alibaba Cloud DTS interface to achieve database synchronization Jul 05, 2023 am 11:22 AM

Java code example: Using Alibaba Cloud DTS interface to achieve database synchronization Introduction: With the rapid development of cloud computing and big data, database synchronization has become one of the indispensable needs of many enterprises. Alibaba Cloud's Data Transfer Service (DTS) provides powerful database synchronization functions, which can help enterprises quickly and efficiently achieve data synchronization between different databases. This article will introduce how to use the Alibaba Cloud DTS interface to achieve database synchronization, and provide corresponding Java code examples. 1. Preparation: Before starting, we need to

Is there an online tool that can convert Python code into Java code? Is there an online tool that can convert Python code into Java code? Aug 27, 2023 pm 07:01 PM

Python and Java are two programming languages ​​widely used in the software development industry. They each have a range of advantages and disadvantages and are suitable for different types of projects. Python is known for its ease of use and readability, while Java is known for its robustness and performance. One of the main differences between Python and Java is the way they are written. Python has a looser syntax that makes writing and understanding code easier, while Java has a stricter syntax that can make writing and understanding code somewhat challenging. Despite these changes, many developers find themselves in a situation where they need to convert Python code to Java code, for example, to work on a project that requires Java

How to use Java code to implement fuzzy search for locations on Baidu Maps? How to use Java code to implement fuzzy search for locations on Baidu Maps? Jul 30, 2023 pm 11:31 PM

How to use Java code to implement fuzzy search for locations on Baidu Maps? With the development of the Internet, people's demand for geographical location information is also getting higher and higher. For example, we may need to search for nearby restaurants, hotels or other specific places by keywords. Baidu Maps provides rich location search functions, and using Java code combined with Baidu Map API can easily implement fuzzy search for locations. Below we will introduce how to use Java code to implement fuzzy search for locations on Baidu Maps. First, we need

What should I do if my java code cannot run? What should I do if my java code cannot run? Mar 21, 2024 pm 04:46 PM

Java code may fail to run for a variety of reasons. Common solutions include: checking environment configuration, checking syntax errors, solving class path problems, handling runtime exceptions, managing dependencies, solving permission problems, ensuring sufficient memory, checking IDE configuration, eliminating interference from firewalls or security software, and confirming version compatibility. sex.

See all articles