配置jetty log为Apache log4j

项目最近使用的是jetty服务器,但是使用时发现jetty的日志输出的时间格式和项目定义的格式不太一样.
jetty:yyyy-MM-dd HH:mm:ss.SSS
项目中的配置为:yyyy-MM-dd HH:mm:ss
以前对jetty了解不够,就去一步步的看配置文件,发现jetty的安装目录中有一个文件叫jetty-loggin.xml,但是发现并没有可以指定输出时间格式的位置.然后我找到了jetty默认的日志处理类StdErrLog,发现这样一段代码:

 private void format(StringBuilder buffer, String level, String msg, Object... args)
    {
        long now = System.currentTimeMillis();
        int ms=(int)(now%1000);
        String d = _dateCache.formatNow(now);
        tag(buffer,d,ms,level);
        format(buffer,msg,args);
    }
      private void tag(StringBuilder buffer, String d, int ms, String tag)
    {
        buffer.setLength(0);
        buffer.append(d);
        if (ms > 99)
        {
            buffer.append('.');
        }
        else if (ms > 9)
        {
            buffer.append(".0");
        }
        else
        {
            buffer.append(".00");
        }
        buffer.append(ms).append(tag);

        String name=_printLongNames?_name:_abbrevname;
        String tname=Thread.currentThread().getName();

        int p=__tagpad>0?(name.length()+tname.length()-__tagpad):0;

        if (p<0)
        {
            buffer
            .append(name)
            .append(':')
            .append("                                                  ",0,-p)
            .append(tname);
        }
        else if (p==0)
        {
            buffer.append(name).append(':').append(tname);
        }
        buffer.append(':');

        if (_source)
        {
            Throwable source = new Throwable();
            StackTraceElement[] frames = source.getStackTrace();
            for (int i = 0; i < frames.length; i++)
            {
                final StackTraceElement frame = frames[i];
                String clazz = frame.getClassName();
                if (clazz.equals(StdErrLog.class.getName()) || clazz.equals(Log.class.getName()))
                {
                    continue;
                }
                if (!_printLongNames && clazz.startsWith("org.eclipse.jetty."))
                {
                    buffer.append(condensePackageString(clazz));
                }
                else
                {
                    buffer.append(clazz);
                }
                buffer.append('#').append(frame.getMethodName());
                if (frame.getFileName() != null)
                {
                    buffer.append('(').append(frame.getFileName()).append(':').append(frame.getLineNumber()).append(')');
                }
                buffer.append(':');
                break;
            }
        }

        buffer.append(' ');
    }

可以看出日志的输出格式是固定的,所以默认的日志输出是不能用了,我google了一下,jetty是支持其他的日志框架的:
参考jetty文档:http://www.eclipse.org/jetty/documentation/9.3.x/example-logging-log4j.html

文档中的大概步骤:
1.在jetty的lib目录下新增logging目录,把slf4j-api-1.6.6.jar,log4j-1.2.17.jar,slf4j-log4j12-1.6.6.jar放进去
在jetty的resources目录下新增log4j.properties配置文件,和jetty-logging.properties,其中弄log4j的配置没有什么太大的出入,主要是在jetty-logging.properties中添加:

# Configure Jetty for SLf4j Logging
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog

最后一步:在start.ini文件中添加一行:

--module=logging

然后重启你的jetty,就可以了.
但是需要注意这里输出的日志只有jetty的日志,不会有项目的日志的.

还有一种方式是参考别人的配置方式,自己又修改了一下成功的: 如下:
1.在jetty的lib/ext目录中添加上述三个jar包
2.在resources中添加log4j.properties
3.在jetty的etc目录中找到jetty.conf文件,并注释掉jetty-logging.xml这行
# ========================================================
# jetty.conf Configuration for jetty.sh script
# --------------------------------------------------------
# This file is used by the jetty.sh script to provide 
# extra configuration arguments for the start.jar command
# created by that script.
# 
# Each line in this file becomes an arguement to start.jar
# in addition to those found in the start.ini file
# =======================================================
#jetty-logging.xml
jetty-started.xml

然后就重启jetty就可以了,当然这种配置同样只会输出jetty的日志.

那么怎么把项目的日志和jetty的日志放在一起呢,(项目中采用的log4j,非log4j未测试)我们想的的办法就是项目中的log4j配置文件和jetty的配置文件指定同一个输出文件,采用追加方式就行了.

(本人新手程序猿,如有错误希望指正,共同学习!)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值