为什么Spring Boot Batch应用程序无法打印到控制台?

时间:2019-03-25 12:45:06

标签: spring spring-boot spring-batch slf4j spring-logback

我正在使用带有弹簧批处理的sprint引导启动器。我记录所有内容以记录文件和控制台。我在lomback的类级别将注释用作@Slf4j。 在日志文件中,一切正常,但请注意是从控制台出来的。 我使用perl文件启动外壳脚本文件,然后外壳文件执行包含所有批处理应用程序的jar文件。

我的perl文件启动一个shell文件,然后该shell文件将jar文件分层。

perl:

$command = ". $ENV{'TARI_SOMPROGS'}$bin_pad"."StartAggr.sh $jaa $maan $ENV{'TARI_DBNAAM'} $td 1 1>&2 ";
      start_ica_shell($command);

外壳文件:

java -jar ${TARI_SOFTWARE}/aggre/jar/spring-batch-article-0.0.1-SNAPSHOT.jar $JAA $MAAN $DATABASE $TARI $AGGR_TYPE

为什么它不能打印到控制台?

我尝试了什么: 首先,我将logback-spring.xml用于application.properties中的配置,如下所示:

<configuration scan="true" debug="true">
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="guru.springframework.controllers" level="WARN" additivity="false">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="FILE"/>
</logger>
<logger name="guru.springframework.helpers" level="WARN" additivity="false">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="FILE"/>
</logger>

application.properties

    spring.application.name=aggregation
spring.main.banner-mode=log
debug=true
spring.batch.job.enabled=false
spring.output.ansi.enabled=detect
logging.level.root=WARN
logging.level.com.ava.batch=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.sql=DEBUG
logging.level.org.hibernate=ERROR
logging.file.max-size = 11
logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
logging.file =/ava/tar/print/log/aggregatie/${spring.application.name}.log
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"

和我的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.toptal</groupId>
    <artifactId>spring-batch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-batch-article</name>
    <description>Demo project for Spring Batch</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter</artifactId>-->
            <!--<exclusions>-->
                <!--<exclusion>-->
                    <!--<groupId>org.springframework.boot</groupId>-->
                    <!--<artifactId>spring-boot-starter-logging</artifactId>-->
                <!--</exclusion>-->
            <!--</exclusions>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-cli -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-cli</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!--Logback Begin-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!--Logback End-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.3.RELEASE</version>
            </plugin>
        </plugins>
    </build>

</project>

其次我使用了logback.xml,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
    <property name="LOG_PATH" value="/ava/tari/print/log/aggregatie" />
    <property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
    <timestamp key="timestamp-by-second" datePattern="yyyyMMdd'T'HHmmss"/>
    <appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
        <layout>
            <pattern>%msg%n</pattern>
            <!--<pattern>%d{yyyy/MM/dd uu:mm:ss, SSS} [%thread] %-5level %logger{36} -%m %throwable{0} %n</pattern>-->
        </layout>j
    </appender>
    <appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_PATH}/logfile-${timestamp-by-second}.log</file>
        <encoder>
            <!--<pattern>%msg%n</pattern>-->
            <pattern>%d{yyyy/MM/dd uu:mm:ss, SSS} [%thread] %-5level %logger{36} -%m %throwable{0} %n</pattern>
            <outputPatternAsHeader>true</outputPatternAsHeader>
        </encoder>
    </appender>
    <appender name="RollingFile-Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/rollingfile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <totalSizeCap>1KB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <!--<pattern>%msg%n</pattern>-->
            <pattern>%d{yyyy/MM/dd uu:mm:ss, SSS} [%thread] %-5level %logger{36} -%m %throwable{0} %n</pattern>
        </encoder>
    </appender>
    <appender name="Async-Appender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RollingFile-Appender" />
    </appender>

    <logger name="be.kava.batch.aggregation"  level="info" additivity="false">
        <appender-ref ref="Console-Appender" />
        <appender-ref ref="File-Appender" />
        <appender-ref ref="Async-Appender" />
    </logger>
    <root>
        <appender-ref ref="Console-Appender" />
    </root>
</configuration>

1 个答案:

答案 0 :(得分:0)

在开始如下所示的mijn shell命令之前,我在perl文件中添加了select STDOUT;。 这样就解决了将输出重定向回正常状态的问题。

select STDOUT;
$command = ". $ENV{'TARI_SOMPROGS'}$bin_pad"."StartAggr.sh $jaa $maan $ENV{'TARI_DBNAAM'} $td 1 1>&2 ";
      start_ica_shell($command);