2

我的 spring-shell.log 有问题。不是什么大问题,但真的很烦人。一旦我从工作空间之外运行我的应用程序,spring shell 就会尝试将 spring-shell.log 保存在受 Windows 保护的目录中。这导致没有 Shelö 历史记录。现在我只是想将文件保存在其他地方

我最喜欢的方法是通过 application.properties 更改位置。但我不知道这是否有属性。我也可以更改 logback-spring.xml。

java.nio.file.AccessDeniedException: C:\Windows\spring-shell.log
        at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) ~[na:1.8.0_191]
        at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) ~[na:1.8.0_191]
        at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) ~[na:1.8.0_191]
        at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source) ~[na:1.8.0_191]
        at java.nio.file.spi.FileSystemProvider.newOutputStream(Unknown Source) ~[na:1.8.0_191]
        at java.nio.file.Files.newOutputStream(Unknown Source) ~[na:1.8.0_191]
        at java.nio.file.Files.newBufferedWriter(Unknown Source) ~[na:1.8.0_191]
        at java.nio.file.Files.newBufferedWriter(Unknown Source) ~[na:1.8.0_191]
        at org.jline.reader.impl.history.DefaultHistory.save(DefaultHistory.java:121) ~[jline-3.4.0.jar!/:na]
        at org.jline.reader.impl.history.DefaultHistory.add(DefaultHistory.java:248) ~[jline-3.4.0.jar!/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_191]
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) [spring-aop-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) [spring-aop-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
        at com.sun.proxy.$Proxy52.add(Unknown Source) [na:na]
        at org.jline.reader.impl.LineReaderImpl.finishBuffer(LineReaderImpl.java:864) [jline-3.4.0.jar!/:na]
        at org.jline.reader.impl.LineReaderImpl.readLine(LineReaderImpl.java:557) [jline-3.4.0.jar!/:na]
        at org.jline.reader.impl.LineReaderImpl.readLine(LineReaderImpl.java:390) [jline-3.4.0.jar!/:na]
        at org.springframework.shell.jline.InteractiveShellApplicationRunner$JLineInputProvider.readInput(InteractiveShellApplicationRunner.java:115) [spring-shell-core-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
        at org.springframework.shell.Shell.run(Shell.java:129) [spring-shell-core-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
        at org.springframework.shell.jline.InteractiveShellApplicationRunner.run(InteractiveShellApplicationRunner.java:84) [spring-shell-core-2.0.1.RELEASE.jar!/:2.0.1.RELEASE]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:770) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
        at com.provinzial.convenience.tools.Application.main(Application.java:12) [classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_191]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47) [convenience-tools-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:86) [convenience-tools-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [convenience-tools-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [convenience-tools-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]

我很感激任何提示。多谢你们。最好的问候,塞巴斯蒂安

4

1 回答 1

3

在您的 Spring 项目中,只需像这样创建一个新类:

package com.example.shell.demo.config;

import org.jline.reader.History;
import org.jline.reader.LineReader;
import org.jline.reader.impl.history.DefaultHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;

import java.io.IOException;
import java.nio.file.Paths;

@Configuration
public class HistoryConfiguration {

    @Autowired
    private History history;

    @Bean
    public History history(LineReader lineReader, @Value("${app.history.file}") String historyPath) {
        lineReader.setVariable(LineReader.HISTORY_FILE, Paths.get(historyPath));
        return new DefaultHistory(lineReader);
    }

    @EventListener
    public void onContextClosedEvent(ContextClosedEvent event) throws IOException {
        history.save();
    }
}

然后,在您的 application.properties 文件中,创建一个像这样的键/值对

app.history.file=<path to your log file>

原始解决方案发布在这里:https ://github.com/spring-projects/spring-shell/issues/220 。我已经使用 Spring Shell 2.0.0-RELEASE 对其进行了测试,并且效果很好。

希望有帮助。

于 2020-01-16T15:31:58.627 回答