log42 如何自定义logger

自定义logger,个人理解就是:logger类里记录的日志单独打印到指定位置

自定义logger

@Slf4j
public class logUtils {
    public static void logInfo(String message) {
        log.info(message);
    }
}

Log4j2.xml 配置


<configuration status="info" monitorInterval="10">
    <properties>
        <property name="LOG_HOME">./applog/logsproperty>
        <Property name="FILE_NAME" value="practisesvr"/>
        <Property name="LOG_PATTERN"
                  value="[%d{yyyy-MM-dd HH:mm:ss.SSS_SSS}] [%-5p] [%thread] [%file:%line] → [%replace{%enc{%m}{CRLF}}{\\r|\\n|%0D|%0A|%0a|%0d}{}]%n"/>
    properties>

    <appenders>
        <console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        console>

        <RollingFile name="SIZE_BASED_TRIGGERING"
                     fileName="${LOG_HOME}/${FILE_NAME}.log"
                     filePattern="${LOG_HOME}/${FILE_NAME}_%d{yyyy-MM-dd-HH}_%i.log.gz"
                     createOnDemand="true">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10K"/>
            Policies>
            <DefaultRolloverStrategy fileIndex="nomax">
                <Delete basePath="${LOG_HOME}" maxDepth="2">
                    <IfFileName glob="*.log.gz">
                        <IfAny>
                            <IfAccumulatedFileSize exceeds="100M"/>
                            <IfAccumulatedFileCount exceeds="100"/>
                            <IfLastModified age="30d"/>
                        IfAny>
                    IfFileName>
                Delete>
            DefaultRolloverStrategy>
        RollingFile>
    appenders>

    <loggers>
        <root level="all">
            <AppenderRef ref="CONSOLE"/>
        root>

        <logger name="com.zhangziwa.practisesvr.utils.log.logUtils" level="info" additivity="false">
            <AppenderRef ref="CONSOLE"/>
            <AppenderRef ref="SIZE_BASED_TRIGGERING"/>
        logger>
    loggers>
configuration>

使用

String apiJson = logUtils.buildApiJsonLog(request, response, executionCost);
logUtils.logInfo(apiJson);

你可能感兴趣的:(日志,java,开发语言)