Spring Boot Logging属性与Log4J 2属性

时间:2016-03-31 03:24:56

标签: spring-boot log4j2

在Spring Boot Web应用程序中,我在application.properties中有以下日志记录属性。

logging.level.guru.springframework.controllers=DEBUG
logging.level.org.springframework.web=DEBUG
logging.file=logs/spring-boot-logging.log

事情很好 - 内部Spring Boot和应用程序的DEBUG消息都记录到logs / spring-boot-logging.log中。但是,即使我添加了具有不同日志记录级别的log4j2-spring.xml,仍然会选择application.properties的级别。

我的log4j2-spring.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
  <Properties>
    <Property name="log-path">applogs</Property>
</Properties>
<Appenders>
    <Console name="Console-Appender" target="SYSTEM_OUT">
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>>
        </PatternLayout>
    </Console>
    <File name="File-Appender" fileName="${log-path}/app_log.log" >
        <PatternLayout>
            <pattern>
                [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
        </PatternLayout>
    </File>
</Appenders>
<Loggers>
    <Logger name="org.springframework.web" level="info">
        <AppenderRef ref="File-Appender"/>
    </Logger>
    <Logger name="guru.springframework.controllers" level="info">
        <AppenderRef ref="File-Appender"/>
    </Logger>
    <Root level="info">
        <AppenderRef ref="Console-Appender"/>
    </Root>
  </Loggers>
</Configuration>

使用这些设置,debug(在application.properties中定义)消息将发送到applogs / app_log.log(在log4j2-spring.xml中定义)

在应用程序属性中注释掉logging.level。*时,将使用log4j2-spring.xml中的日志级别(info)。我假设默认情况下,application.properties的属性优先于log4j2-spring.xml。有没有什么办法可以配置Spring Boot来使用log4j2-spring.xml属性而不是application.properties。另外,如果application.properties具有更高的优先级,为什么要使用log4j2-spring.xml的文件追加器而不是logging.file中的文件追加器?

1 个答案:

答案 0 :(得分:0)

如果您通过此Spring Boot属性定义它,则应该使用您的配置文件:

 <div id="mypage">
@model IEnumerable<MVC5WAuth.Models.Employee>

@{
    ViewBag.Title = "Index";
}

<h2>Index 1</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FullName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FullName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>


<script type="text/javascript">


    $(window).scroll(function () {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            // ajax call get data from server and append to the div



            var ajax_image = "<img src='../Content/loading.GIF' >";
            $('#mypage').html(ajax_image);
            var params = '&starting=' + @ViewBag.starting + '&takes=' + @ViewBag.takes;
            $.ajax({
                url:'@Url.Action("Index", "Employees")',
                type: "POST",
                data: params,

            })
            .done(function (r) {

                $('#mypage').html(r);
            });

        }
    });

</script>