首页 \ 问答 \ 无法记录Hibernate查询log4j(can't log Hibernate queries log4j)

无法记录Hibernate查询log4j(can't log Hibernate queries log4j)

我正在尝试将hibernate查询记录到webapp特定的日志文件中。 我可以记录任何消息,但我仍然无法记录休眠查询。
它们正在server.log内部进行记录,即使是在不同的文件中,但我需要在同一台服务器上运行的每个应用程序的特定于应用程序的日志。
我正在使用log4j 1.2 ,我可以创建日志文件,记录传递给控制台但是hibernate查询的每个信息。
database.properties文件中,我找到了hibernate.show_sql=true ,它负责控制台中的日志输出。 这是我引用的那种控制台的日志输出:

16:10:3​​5,827 INFO [stdout](http-localhost-127.0.0.1-8080-1)Hibernate:select [...](这里输出正确的查询通过,准备语句的?

无论如何,我需要的是记录输出。 我尝试了不同的log4j.xml设置,但仍然没有成功,它会创建日志文件但不会记录这些输出。 以下是当前的设置,即将所有内容记录在同一个文件中,当我能够记录甚至是将要分离日志的信息时。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mainFileAppender2" class="org.apache.log4j.RollingFileAppender">
   <param name="append" value="false" />
   <param name="maxFileSize" value="100MB" />
   <param name="maxBackupIndex" value="50" />
    <param name="File" value="${webapp.root}/WEB-INF/logs/mainCORE.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
    </layout>
</appender>

<appender name="journaldev-hibernate2" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${webapp.root}/WEB-INF/logs/hib-queriesCORE.log" />
    <param name="Append" value="false"/><!--value="true" /-->
    <param name="ImmediateFlush" value="true" />
    <param name="MaxFileSize" value="200MB" />
    <param name="MaxBackupIndex" value="50" />

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
    </layout>
</appender>
<root>
    <priority value="debug"></priority>
<!--    <appender-ref ref="stdout"/>  -->
    <appender-ref ref="mainFileAppender2"/>
</root>

该配置会记录除这些输出以外的所有内容 在server.log中记录如下:

16:10:3​​5,827 INFO [stdout](http-localhost-127.0.0.1-8080-1)Hibernate:select [...](查询的其余部分)


I am trying to log hibernate queries into a webapp specific log file. I can log any message but I am still unable to log hibernate queries.
Them are being logged inside the server.log, even in a different file, but I need an app-specific log for each app running on the same server.
I am using log4j 1.2, I am able to create the log files, to log every information passed to console but hibernate queries.
Inside a database.properties file I found the hibernate.show_sql=true that is responsible for the logging output in console. This is the kind of console's logging output I refer:

16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](here is outputted the correct query passed, with the ? of the prepared statement)

Anyway, what I need is to log that output. I tried different log4j.xml settings, but still with no success, it creates log files but does not log those outputs. Here is the current setting, that is logging everything inside the same file, when I will be able to log even those information I will go on separating the logs.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mainFileAppender2" class="org.apache.log4j.RollingFileAppender">
   <param name="append" value="false" />
   <param name="maxFileSize" value="100MB" />
   <param name="maxBackupIndex" value="50" />
    <param name="File" value="${webapp.root}/WEB-INF/logs/mainCORE.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
    </layout>
</appender>

<appender name="journaldev-hibernate2" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${webapp.root}/WEB-INF/logs/hib-queriesCORE.log" />
    <param name="Append" value="false"/><!--value="true" /-->
    <param name="ImmediateFlush" value="true" />
    <param name="MaxFileSize" value="200MB" />
    <param name="MaxBackupIndex" value="50" />

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
    </layout>
</appender>
<root>
    <priority value="debug"></priority>
<!--    <appender-ref ref="stdout"/>  -->
    <appender-ref ref="mainFileAppender2"/>
</root>

This configuration logs everything but those outputs. In the server.log are logged as follows:

16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](rest of the query)


原文:https://stackoverflow.com/questions/42395740
更新时间:2023-07-13 16:07

最满意答案

这里有几件事。

首先,Moment的isBetween()中给出的日期是独占的,而datePickerminDatemaxDate是包含日期。 因此,您需要传递其他参数以在范围内包含这些最小和最大日期:

cDate.isBetween(minDate, maxDate, null, '[]')

其次,如果您正在寻找即时反馈,以确定日期是否在范围内,那么您需要将$setValidity包含在scope.$apply function。

没有查看Datepicker Popup的JavaScript,我不能确切地说明为什么需要,但从实验中我可以看到,它只在弹出窗口关闭时应用ng-invalid-validbound属性(而ng-valid-validbound是一旦有效性设置为真,就应用)。 我认为这是表现的方式,因为无论如何您都无法使用弹出窗口选择超出界限的日期,您只能以其他方式 - 从无效日期到使用弹出窗口选择有效日期。

这或多或少是我如何做到的:

var dateValidate = function() {
  return {
    require: 'ngModel',
    link: function(scope, elem, attr, ngModel) {
      elem.bind('keyup', function(value) {

        var atrvls = scope.$eval(attr.datepickerOptions),
          maxDate = moment(atrvls.maxDate, 'DD/MM/YYYY'),
          minDate = moment(atrvls.minDate, 'DD/MM/YYYY'),
          cDate = moment(elem.val(), 'DD/MM/YYYY', true);

        if (!cDate.isValid()) {
          scope.$apply(ngModel.$setValidity('validbound', null));
          return false;
        }

        if (!cDate.isBetween(minDate, maxDate, null, '[]')) {
          scope.$apply(ngModel.$setValidity('validbound', false));
        } else {
          scope.$apply(ngModel.$setValidity('validbound', true));
        }

      });
    }
  };
};

这里是一个plunker: http ://plnkr.co/edit/toXpEWNa99o7Gfs69KfZ?p=preview


There's a couple of things here.

First, the dates given in Moment's isBetween() are exclusive, whereas the datePicker's minDate and maxDate are inclusive dates. So you'll want to pass additional parameters to include these min and max dates within the range:

cDate.isBetween(minDate, maxDate, null, '[]')

Secondly, if you're looking for immediate feedback for whether the date is within the range then you'll need to wrap $setValidity inside a scope.$apply function.

Without looking at the javascript for the Datepicker Popup I can't say exactly why that is needed, but from experiments I can see that it only applies the ng-invalid-validbound attribute when the popup closes (whereas the ng-valid-validbound is applied as soon as validity is set to true). I assume this is this way for performance, since you can't select an out-of-bounds date using the popup anyway, you can only go the other way - from an invalid date to selecting a valid date using the popup.

This is more or less how I'd do it:

var dateValidate = function() {
  return {
    require: 'ngModel',
    link: function(scope, elem, attr, ngModel) {
      elem.bind('keyup', function(value) {

        var atrvls = scope.$eval(attr.datepickerOptions),
          maxDate = moment(atrvls.maxDate, 'DD/MM/YYYY'),
          minDate = moment(atrvls.minDate, 'DD/MM/YYYY'),
          cDate = moment(elem.val(), 'DD/MM/YYYY', true);

        if (!cDate.isValid()) {
          scope.$apply(ngModel.$setValidity('validbound', null));
          return false;
        }

        if (!cDate.isBetween(minDate, maxDate, null, '[]')) {
          scope.$apply(ngModel.$setValidity('validbound', false));
        } else {
          scope.$apply(ngModel.$setValidity('validbound', true));
        }

      });
    }
  };
};

Here's a plunker: http://plnkr.co/edit/toXpEWNa99o7Gfs69KfZ?p=preview

相关问答

更多
  • 因为您将require属性指定为数组,所以angular将为您的链接函数提供一个数组作为结果。 该阵列将包含请求的控制器。 如果您只需要ngModel控制器,则可以将其作为字符串传递给需要的人员 。 否则,你需要在给你的链接函数的数组内引用它。 require 需要另一个指令并将其控制器作为链接函数的第四个参数。 require属性可以是字符串,数组或对象: 一个包含要传递给链接函数的指令名称的字符串 一个包含要传递给链接函数的指令名称的数组。 传递给链接函数的参数将是一个控制器数组,其顺序与require ...
  • 您正在查看正确的文档,但可能只是您有点困惑。 $modelValue和$viewValue有一个明显的区别。 就是这样 如上所述: $viewValue:视图中的实际字符串(或Object)值。 $modelValue:模型中的值,控件被绑定。 我会假设你的ngModel是指一个元素...? 所以你的有一个字符串值,它显示给用户,对吧? 但实际的模型可能是该字符串的其他版本。 例如,输入可能显示字符串'200'但是 (例如)实际上将包 ...
  • 在野外已经有一些这样的例子,阅读和学习。 每个人都有一些赞成/赞成。 https://docs.angularjs.org/api/ng/type/ngModel.NgModelController http://bit.ly/1o4Kh2c http://textangular.com/ https://github.com/angular/angular.js/issues/528 https://github.com/akatov/angular-contenteditable 提示: 显示和编辑样式 ...
  • 能够解决这个问题。 当异步调用正在进行时,提交一个临时值,如“VALIDATION_PENDING”,当验证完成时,执行ngModel。$ commitViewValue(true)。 我正在传递真实再次触发解析器。 你的解析器应该处理ngModel。$ modelValue ==='VALIDATION_PENDING'时的情况。 在这种情况下,不要进行异步调用,只需从Parser返回$ viewValue即可。 码: module.directive('shouldBeUnique', ['$parse ...
  • 如果您只是在指令中寻找双向绑定数据,则在scope使用= ,并且您需要在链接函数中使用$watch (观察范围上变量的名称): scope.$watch('ngModel', function (/* ... */) { /* ... */ }); 如果ng-model是属性的名称,那么指令的消费者可能会感到困惑,所以我建议选择一个不同的名称(最好不要用ng-作为前缀)。 If you're just looking to two-way bind data in a directive, using = ...
  • 这里有几件事。 首先,Moment的isBetween()中给出的日期是独占的,而datePicker的minDate和maxDate是包含日期。 因此,您需要传递其他参数以在范围内包含这些最小和最大日期: cDate.isBetween(minDate, maxDate, null, '[]') 其次,如果您正在寻找即时反馈,以确定日期是否在范围内,那么您需要将$setValidity包含在scope.$apply function。 没有查看Datepicker Popup的JavaScript,我不 ...
  • 由于此更改 , 此操作在1.2.0时已修复: 修复了隔离范围泄漏到同一元素上的其他指令的问题。 隔离范围现在仅可用于请求它的isolate指令及其模板。 在1.2.0之前,如果对元素的任何指令请求隔离范围,则该元素上的所有指令都共享该范围。 在您的情况下,这导致input指令使用您请求指令的隔离范围,而不是html

    Filename is: {{ file.name }}

    的父范围。 因此file undefined因为file位于子范围内 通过使用$setViewValue下面的这一行将$vi ...
  • 来自AngularDoc 从解析器返回undefined意味着发生了解析错误。 在这种情况下,除非将ngModelOptions.allowInvalid设置为true,否则不会运行$ validators并将ngModel设置为undefined。 解析错误存储在ngModel。$ error.parse中。 因此,请在输入字段中尝试此操作 From AngularDoc Returning unde ...
  • 为什么ngModel是空的? 在指令上使用require时,控制器作为第4个参数传递给链接函数 。 在您的代码中,您尝试将其作为编译函数的参数引用。 控制器仅在链接阶段之前被实例化,因此无论如何它永远不会被传递到编译函数中。 更大的问题是require只能获得相同元素的控制器( { require: 'ngModel' } )或父元素( { require: '^ngmodel' } )。 但是您需要从子元素(模板内)引用控制器。 如何获得ngModel ? 不要使用require,因为你无法使用它来获取子 ...
  • Angular在一个元素中收集指令(让我们称之为“指令发现”阶段), 然后调用编译函数; myDatepicker的编译功能添加了一个属性,如果存在于进程的开头,则会将其解释为指令。 但是这个元素的“指令发现”阶段结束了,属性只是一个属性。 但是,在您的情况下,很容易通过以下方式之一来纠正这种情况: myDatepicker可以包含myDatepicker的功能。 最简单的情况,如果不单独使用myTestValidation ,请使用此方法。 myTestValidation的功能将导出到服务, myTes ...

相关文章

更多

最新问答

更多
  • 您如何使用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)