logback 打印出来的日志上传ftp显示程序被占用(贴的代码不重要,和网上代码是一样的) 5C

请问各位大佬,logback打印是个什么原理呢,看情况就像“字节流”(这个表述对吗)一直开着,没有关闭似的,也无法修改日志内容(修改之后,一保存就提示程序占用)

如果像这样“打印日志的服务”一直开着的情况下,那以后并发量大的时候,资源不就很快就被消耗完了。

请问要怎么配置才能打印完一段日志就关闭“服务”以至于不会在操作的时候,显示程序在使用中。

项目运行的日志打印:
logback.xml
的配置如下(和网上配置一样):

 <?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright 2010-2011 The myBatis Team
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<configuration debug="false">
    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
    <!-- /U01/apps/tomcat-statusServer/statusServer_logs -->
    <!-- <property scope="context" name="LOG_HOME" value="/tomcat-statusServer/statusServer_logs" />  -->
    <property scope="context" name="LOG_HOME" value="/U01/apps/tomcat-statusServer/statusServer_logs" /> 
    <!-- 控制台输出 -->   
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 
             <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> 
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>   
        </encoder> 
    </appender>
    <!-- 按照每天生成日志文件 -->   
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">   
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern> 
            <!--日志文件保留天数-->
            <MaxHistory>30</MaxHistory>
            <maxFileSize>10MB</maxFileSize>
        </rollingPolicy>   
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> 
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>   
        </encoder> 
    </appender> 
   <!-- show parameters for hibernate sql 专为 Hibernate 定制 --> 
    <logger name="org.hibernate.type.descriptor.sql.BasicBinder"  level="TRACE" />  
    <logger name="org.hibernate.type.descriptor.sql.BasicExtractor"  level="DEBUG" />  
    <logger name="org.hibernate.SQL" level="DEBUG" />  
    <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" />
    <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" />  

    <!--myibatis log configure--> 
    <logger name="com.apache.ibatis" level="TRACE"/>
    <logger name="java.sql.Connection" level="DEBUG"/>
    <logger name="java.sql.Statement" level="DEBUG"/>
    <logger name="java.sql.PreparedStatement" level="DEBUG"/>

    <!-- 日志输出级别 -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root> 
</configuration>

重写日志打印方法,以实现方法调用日志打印方法的配置:
(我项目里还有通过方法调用logback打印,实现日志按照要求输出到我需要的文件)

调用:

 Logger logger = LoggerBuilder.getLogger(uniteStr);
                logger.info(logAgora);

工具类:

 package com.bell.tempStatus.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
import ch.qos.logback.core.util.FileSize;
import ch.qos.logback.core.util.OptionHelper;  
public class LoggerBuilder {  

    private static final Map<String,Logger> container = new HashMap<>();  

    public static Logger getLogger(String name) {

        Logger logger = container.get(name);  
        if(logger != null) {  
            return logger;  
        }  
        synchronized (LoggerBuilder.class) {  
            logger = container.get(name);  
            if(logger != null) {  
                return logger;  
            }  
            logger = build(name);  
            container.put(name,logger);  
        }  
        return logger;  
    }  

    private static Logger build(String name) {
        //TODO 重写这个方法
        //name = ua + rid + uid
        String[] split = name.split("_");

        String replace = name.replace("_", "");
        int count = name.length() - replace.length();

        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();  
        Logger logger = context.getLogger("FILE-" + name);  
        logger.setAdditive(false);  
        RollingFileAppender appender = new RollingFileAppender();  
        appender.setContext(context);  
        appender.setName("FILE-" + name);
        if(count == 3) {
            //appender.setFile(OptionHelper.substVars("E:/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] +"_"+ getSysYearMonthDay()+ "_agora" + ".log",context));
            appender.setFile(OptionHelper.substVars("/U01/apps/tomcat-statusServer/statusServer_logs/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] +"_"+ getSysYearMonthDay()+ "_agora" + ".log",context));
        }
        else {
            appender.setFile(OptionHelper.substVars("/U01/apps/tomcat-statusServer/statusServer_logs/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] +"_"+ getSysYearMonthDay() + ".log",context));
            //appender.setFile(OptionHelper.substVars("E:/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] +"_"+ getSysYearMonthDay() + ".log",context));
        }
        appender.setAppend(true);  
        appender.setPrudent(false);  
        SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy();  
        String fp = OptionHelper.substVars("/U01/apps/tomcat-statusServer/statusServer_logs/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] +"_"+ ".%d{yyyy-MM-dd}.%i",context);
        //String fp = OptionHelper.substVars("E:/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] +"_"+ ".%d{yyyy-MM-dd}.%i",context);  
        if(count == 3) {
            //fp = OptionHelper.substVars("E:/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] + "_" + "_agora" + ".%d{yyyy-MM-dd}.%i",context);
            fp = OptionHelper.substVars("/U01/apps/tomcat-statusServer/statusServer_logs/" + split[0] + "/"+ getSysYear() + "/" + getSysMonth() + "/" + split[1] + "_" + split[2] + "_" + "_agora" + ".%d{yyyy-MM-dd}.%i",context);
        }
        policy.setMaxFileSize(FileSize.valueOf("128MB"));  
        policy.setFileNamePattern(fp);  
        policy.setMaxHistory(15);  
        policy.setTotalSizeCap(FileSize.valueOf("32GB"));  
        policy.setParent(appender);  
        policy.setContext(context);  
        policy.start();  

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();  
        encoder.setContext(context);  
        encoder.setPattern("%d{yyyy-MM-dd/HH:mm:ss.SSS}|%X{localIp}|[%t] %-5level %logger{50} %line - %n%m%n");  
        encoder.start();  

        appender.setRollingPolicy(policy);  
        appender.setEncoder(encoder);  
        appender.start();  
        logger.addAppender(appender);  

        return logger;  
    }  

    public static String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }

    public static int getSysMonth() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH )+1;
        return month;
    }

    public static String getSysYearMonthDay() {
        Date date = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String dateNowStr = sdf.format(date);  
        return dateNowStr;
    }


}

各位大佬帮忙看一下,谢谢大家了。

1个回答

你想要把logback的日志文件上传到ftp其实是可以的,但是你需要改变下策略,由于logback当前的正在处理的日志文件是处于服务器打开状态,确实不允许别的程序去
操作它,这个时候你可以将日志文件按天存储,然后上传ftp的时候,判断下只上传今天之前的日志文件就可以了,忘采纳!

liuxinnidongde
IT-JAVA小刘 回复qq_25502529: 那就每天的再切成每100M一个文件的方式就行,上传ftp,上传之前已处理的日志文件就行,上传完同时删除本地的
大约 2 年之前 回复
qq_25502529
吴大宝藏男孩 谢谢回答,不能通过配置关闭吗?因为我有通过重写logback的一些方法实现根据传入的条件实现不同的日志文件打印,如果不能关闭的话,以后用户量变大的情况下,哪怕一天之内也会很占用资源
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问