首页 \ 问答 \ 应用程序log4j.properties未加载(Application log4j.properties not loaded)

应用程序log4j.properties未加载(Application log4j.properties not loaded)

我在amazon linux(版本201303)上安装了一个干净的tomcat 7,其中一个应用程序部署为ROOT。 log4j-1.2.17.jar驻留在WEB-INF / lib中。 WEB-INF / classes中有一个log4j.properties文件,其中包含以下内容:

log4j.rootLogger=DEBUG,A1, A2

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=DEBUG

log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd}-%t-%x-%-5p-%-10c:%m%n

log4j.appender.A2.File=${catalina.home}/logs/app.log
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.DatePattern='.' yyyy-MM-dd
log4j.appender.A2.MaxFileSize=10MB
log4j.appender.A2.MaxBackupIndex=99
log4j.appender.A2.Append=true

log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS}-%t-%x-%-5p-%-10c:%m%n

在应用程序中,log4j实例化为

static Logger logger = Logger.getLogger(ClassName.class.getName());

app.log永远不会被创建。 如果我手动创建它,它永远不会被写入。 唯一的日志记录是在logs / catalina.out中。 我错过了什么? 我已多次通过http://tomcat.apache.org/tomcat-7.0-doc/logging.html阅读。 以下摘录似乎证实了我的方法:

如果您只想在自己的Web应用程序中使用log4j,则不需要这些步骤。 - 在这种情况下,只需将log4j.jar和log4j.properties放入Web应用程序的WEB-INF / lib和WEB-INF / classes中。

catalina_home / lib中有一个log4j.jar和log4j.xml,以及catalina_home / conf中的log4j.properties。 那些与我的WAR中包含的罐子有冲突吗? 我没有在catalina.out中看到任何错误。

UPDATE

所以看来WEB-INF / classes中的log4j.properties正在加载。 我删除了ConsoleAppender(log4j.appender.A1)并停止出现在catalina.out中的日志消息。 我把它重新放入,日志消息再次出现在catalina.out中。 奇怪的是转换模式似乎与日志不匹配。 不应该%d{yyyy-MM-dd}-%t-%x-%-5p-%-10c:%m%n生成像2013-07-10 INFO ClassName Log message ? 相反,我看到14:39:29,180 INFO [ClassName] Log message

更新2

我找到了答案。 $ {catalina.home} / lib中有一个log4j.xml,它在容器级别配置log4j。 我想如果我真的在Tomcat中做了什么,我会早点把它想出来。 一旦我开始配置lib / log4j.xml,我得到了预期的结果。 我仍然有点困惑为什么我的应用程序继承了该配置而不是使用自己的log4j.properties。 哦,我实现了预期的结果,所以有时间继续前进。


I have a clean tomcat 7 installation on amazon linux (version 201303) with one application deployed as ROOT. log4j-1.2.17.jar resides in WEB-INF/lib. There is a log4j.properties file in WEB-INF/classes containing the following:

log4j.rootLogger=DEBUG,A1, A2

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=DEBUG

log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd}-%t-%x-%-5p-%-10c:%m%n

log4j.appender.A2.File=${catalina.home}/logs/app.log
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.DatePattern='.' yyyy-MM-dd
log4j.appender.A2.MaxFileSize=10MB
log4j.appender.A2.MaxBackupIndex=99
log4j.appender.A2.Append=true

log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS}-%t-%x-%-5p-%-10c:%m%n

In the app, log4j is instantiated with

static Logger logger = Logger.getLogger(ClassName.class.getName());

app.log never gets created. If I create it manually, it is never written to. The only logging going on is in logs/catalina.out. What am I missing? I've read through http://tomcat.apache.org/tomcat-7.0-doc/logging.html several times. The following excerpt seems to confirm my approach:

These steps are not needed if you just want to use log4j in your own web application. — In that case, just put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application.

There is a log4j.jar and log4j.xml in catalina_home/lib, along with a log4j.properties in catalina_home/conf. Could those be conflicting with the jars included in my WAR? I don't see any errors in catalina.out.

UPDATE

So it appears that the log4j.properties in WEB-INF/classes is loading. I removed the ConsoleAppender (log4j.appender.A1) and log messages stopped appearing in catalina.out. I put it back in and the log messages reappeared in catalina.out. What's weird is the conversion pattern doesn't seem to match the log. Shouldn't %d{yyyy-MM-dd}-%t-%x-%-5p-%-10c:%m%n generate something like 2013-07-10 INFO ClassName Log message? Instead I'm seeing 14:39:29,180 INFO [ClassName] Log message.

UPDATE 2

I found my answer. There was a log4j.xml in ${catalina.home}/lib that was configuring log4j at the container level. I guess if I actually new what I was doing in Tomcat I would have figured it out sooner. Once I started configuring lib/log4j.xml I got the expected result. I'm still a little confused why my app inherited that configuration instead of using its own log4j.properties. Oh well, I achieved the intended result so time to move on.


原文:https://stackoverflow.com/questions/17561257
更新时间:2022-06-15 11:06

最满意答案

有些术语是多余的,因为无论如何Java都会采取这种行为。

A·k mod 2^w

在Java中,整数乘法溢出并因此执行mod 2^w (带符号)。 它有一个符号的事实并不重要,如果你正在移动至少一个位。

(w - r)的移位与Java中-r的移位相同(w由类型表示)

private static final int K_PRIME = (int) 2999999929L;

public static int hash(int a, int r) {
   // return (a * K_PRIME % (2^32)) >>> (32 - r);
   return (a * K_PRIME) >>> -r;
}

为64位

private static final long K_PRIME = new BigInteger("9876534021204356789").longValue();

public static long hash(long a, int r) {
    // return (a * K_PRIME % (2^64)) >>> (64 - r);
    return (a * K_PRIME) >>> -r;
}

我已经写了这个例子来说明你可以在BigInteger中做同样的事情,你为什么不做。 ;)

public static final BigInteger BI_K_PRIME = new BigInteger("9876534021204356789");
private static long K_PRIME = BI_K_PRIME.longValue();

public static long hash(long a, int r) {
    // return (a * K_PRIME % (2^64)) >>> (64 - r);
    return (a * K_PRIME) >>> -r;
}

public static long biHash(long a, int r) {
    return BigInteger.valueOf(a).multiply(BI_K_PRIME).mod(BigInteger.valueOf(2).pow(64)).shiftRight(64 - r).longValue();
}

public static void main(String... args) {
    Random rand = new Random();
    for (int i = 0; i < 10000; i++) {
        long a = rand.nextLong();
        for (int r = 1; r < 64; r++) {
            long h1 = hash(a, r);
            long h2 = biHash(a, r);
            if (h1 != h2)
                throw new AssertionError("Expected " + h2 + " but got " + h1);
        }
    }

    int runs = 1000000;
    long start1 = System.nanoTime();
    for (int i = 0; i < runs; i++)
        hash(i, i & 63);
    long time1 = System.nanoTime() - start1;

    long start2 = System.nanoTime();
    for (int i = 0; i < runs; i++)
        biHash(i, i & 63);
    long time2 = System.nanoTime() - start2;
    System.out.printf("hash with long took an average of %,d ns, " +
            "hash with BigInteger took an average of %,d ns%n",
            time1 / runs, time2 / runs);
}

版画

hash with long took an average of 3 ns, \
    hash with BigInteger took an average of 905 ns

Some of the terms are redundant because Java assumes this behaviour anyway.

A·k mod 2^w

In Java, integer multiplication overflows and thus does a mod 2^w (with a sign). The fact that it has a sign doesn't matter if you are then shifting by at least one bit.

Shift of (w - r) is the same as a shift of -r in Java (the w is implied by the type)

private static final int K_PRIME = (int) 2999999929L;

public static int hash(int a, int r) {
   // return (a * K_PRIME % (2^32)) >>> (32 - r);
   return (a * K_PRIME) >>> -r;
}

for 64-bit

private static final long K_PRIME = new BigInteger("9876534021204356789").longValue();

public static long hash(long a, int r) {
    // return (a * K_PRIME % (2^64)) >>> (64 - r);
    return (a * K_PRIME) >>> -r;
}

I have written this example to show you can do the same thing in BigInteger and why you wouldn't. ;)

public static final BigInteger BI_K_PRIME = new BigInteger("9876534021204356789");
private static long K_PRIME = BI_K_PRIME.longValue();

public static long hash(long a, int r) {
    // return (a * K_PRIME % (2^64)) >>> (64 - r);
    return (a * K_PRIME) >>> -r;
}

public static long biHash(long a, int r) {
    return BigInteger.valueOf(a).multiply(BI_K_PRIME).mod(BigInteger.valueOf(2).pow(64)).shiftRight(64 - r).longValue();
}

public static void main(String... args) {
    Random rand = new Random();
    for (int i = 0; i < 10000; i++) {
        long a = rand.nextLong();
        for (int r = 1; r < 64; r++) {
            long h1 = hash(a, r);
            long h2 = biHash(a, r);
            if (h1 != h2)
                throw new AssertionError("Expected " + h2 + " but got " + h1);
        }
    }

    int runs = 1000000;
    long start1 = System.nanoTime();
    for (int i = 0; i < runs; i++)
        hash(i, i & 63);
    long time1 = System.nanoTime() - start1;

    long start2 = System.nanoTime();
    for (int i = 0; i < runs; i++)
        biHash(i, i & 63);
    long time2 = System.nanoTime() - start2;
    System.out.printf("hash with long took an average of %,d ns, " +
            "hash with BigInteger took an average of %,d ns%n",
            time1 / runs, time2 / runs);
}

prints

hash with long took an average of 3 ns, \
    hash with BigInteger took an average of 905 ns

相关问答

更多
  • 关于这个问题的新细节引出了我的答案。 我本来会关闭这个问题,但是我发现了一些有用的东西,所以为了记录起见,我将把它们留在这里。 天下奇闻 根据我的情况,当设置marginTop不适用于任何值。 我不得不使用带负值的layout_marginBottom 。 我的解决方案 我发现在这个布局之外有许多父容器,这个布局被加载到那里。 我不得不对其中的一些测试clipChildren="false" ,但我终于发现了导致问题的原因。 所以这是剪辑儿童和布局边距的组合导致我的问题。 希望有人会从我失去的时间中获益 N ...
  • 打开模式与C fopen() std库函数完全相同。 BSD fopen页定义如下: The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.): ``r'' Open text file for reading. The stream is positioned at the ...
  • 这不是原始的绊倒你的原始字符串; 你误解了os.path.join 。 当组件不以一个开头时, os.path.join 应该添加一个斜杠。 如果斜杠已经存在,它将被视为绝对路径的开头,它会丢弃前面的组件并再次从头开始(除了在Windows上,驱动器号)。 来自文档: 如果组件是绝对路径,则抛弃所有先前组件并从绝对路径组件继续连接。 在Windows上,遇到绝对路径组件(例如,r'\ foo')时,不会重置驱动器号。 如果组件包含驱动器号,则会丢弃所有先前的组件并重置驱动器号。 请注意,由于每个驱动器都有一 ...
  • 一方面,XPath表达式 for t in q.xpath('/../..//w:instrText...'...) 没有意义。 初始/表示“从根节点开始”(即最外层元素的父节点)。 以下步骤, .. ,表示“上下文节点的父节点”。 但是根节点没有父节点。 所以这个表达式什么都不会选。 (实际上,在XPath 1.0中, /..用作指定空节点集的便捷方式。) 相反,你可能想要 for t in q.xpath('../..//w:instrText...'...) 它取自上下文节点的父节点( t的值 ...
  • 我认为一个明智的方法是这样进行: 使用String.split(",")将字符串拆分为字符串数组(称之为labelsIn String.split(",") 。 创建一个ArrayList (称之为labelsOut )来保存结果。 循环遍历labelsIn数组和每个元素: 如果它与模式 [AZ][0-9]+ 匹配,则将其附加到labelsOut列表。 否则,如果它匹配模式([AZ])([0-9]+)-\1([0-9]+) (调用Matcher对象match ),则提取low和high的数字 ...
  • 有些术语是多余的,因为无论如何Java都会采取这种行为。 A·k mod 2^w 在Java中,整数乘法溢出并因此执行mod 2^w (带符号)。 它有一个符号的事实并不重要,如果你正在移动至少一个位。 (w - r)的移位与Java中-r的移位相同(w由类型表示) private static final int K_PRIME = (int) 2999999929L; public static int hash(int a, int r) { // return (a * K_PRIME % ...
  • 看起来你的“RemoteIPHeader”配置不正确。 删除您在那里的内容,并尝试将其设置为: RemoteIPHeader X-Forwarded-For 它应该工作。 希望能帮助到你。 谢谢, It seems like your "RemoteIPHeader" is not configured properly. Remove what you have there and try to set it only as: RemoteIPHeader X-Forwarded-For And it ...
  • 想出来(有点)。 从http://snaps.php.net/获得最新的开发版本(5.3-201105190430)并编译并安装它。 这解决了这个问题(显然他们知道这个问题并让它排队等候)。 谢谢! Figured it out (sort of). Got the latest dev build (5.3-201105190430) from http://snaps.php.net/ and compiled & installed it. That fixed the problem (so app ...
  • 线a←min {wi,W-w}实际上给出'a'的值,该值是w [i]和Ww的最小值。 if w[i] < W-w then a ← w[i] otherwise a ← W-w 对于您可以执行的实现 [w[i], W-w].min The line a ← min{wi, W − w} actually gives a value to 'a' which is minimum value of w[i] and W-w. if w[i] < W-w then a ← w[i] otherwise a ...
  • :help :command 显示:command的文档。 :help i_ctrl-a 显示插入模式下的文档。 如何使用文档是新的vimmer应该学习的最重要的事情 。 认真。 如果您遇到棘手的映射问题,请说b ,您可以这样做 :verbose map b 知道它的作用以及它的定义。 :help :command shows the documentation for :command. :help i_ctrl-a shows the documenta ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)