SpringCloud初学随感 五
LogBack日志整理
logback是log4J的衍生版本,性能要比Log4J高很多.再配置中Log4J使用的是properties配置项,LogBack使用的是xml的配置。LogBack.xml文件的注解如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
<property name="LOG_HOME" value="${catalina.base}/logs/" />
<!-- 控制台输出 -->
<appender name="Stdout" class="ch.qos.logback.core.ConsoleAppender">
<!-- 日志输出编码 -->
<layout class="ch.qos.logback.classic.PatternLayout">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</pattern>
</layout>
</appender>
<!-- 按照每天生成日志文件 -->
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名-->
<FileNamePattern>${LOG_HOME}/server.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</pattern>
</layout>
<!--日志文件最大的大小-->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<!--当单个文件超过10mb就会单独创建一个文件-->
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!-- 日志输出级别 -->
<root level="info">
<!--输出到控制台-->
<appender-ref ref="Stdout" />
<!--输出到文件中-->
<appender-ref ref="RollingFile" />
</root>
<!--日志异步到数据库 -->
<!-- <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
日志异步到数据库
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
连接池
<dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://127.0.0.1:3306/databaseName</url>
<user>root</user>
<password>root</password>
</dataSource>
</connectionSource>
</appender> -->
</configuration>
SpringBoot配置文件多环境配置
不同的环节用到的环境配置是不一样的,所以对不同的环境进行修改时复杂的,所以配置不同多份配置文件,通过命令再启动项目时,指定那种环节的配置文件进行处理。
Profile:代表环境变量
语法:application-{profile}.peoperties
application-dev.properties
application-test.properties
applicat-product.properties
不同的环境运行不同的配置文件
首先在项目中写好这三个不同环境的配置文件,然后将项目打包,打包完在命令行中运行jra包。可以查看不同环境下的代码输出情况。使用下面的代码进行运:
java -jar -XXXXX.jar --spring.profiles.active=dev/test/prod
在idea中打包项目时出现的bug
在将整个项目打包进行黑窗口运行时出现spring-boot-starter-partent bug
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[WARNING] 'dependencies.dependency.version' for org.springframework.boot:spring-boot-starter-web:jar is either LATEST or RELEASE (both of them are being deprecated) @ line 41, column 13
[FATAL] Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.6.RELEASE from/to repo2 (http://repo2.maven.org/maven2/): repo2.maven.org and 'parent.relativePath' points at no local POM @ line 5, column 10
@
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project com.example:demo:0.0.1-SNAPSHOT (D:\ZBresource\WorkSpaces\demo\pom.xml) has 1 error
[ERROR] Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.6.RELEASE from/to repo2 (http://repo2.maven.org/maven2/): repo2.maven.org and 'parent.relativePath' points at no local POM @ line 5, column 10: Unknown host repo2.maven.org -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
出现此bug的原因的maven仓库中存在问题,于是按照网上说的但是没有找到解的方案,于是我将maven 中Setting.xml文件中的镜像都删掉了,只配置了阿里云的镜像,bug就解决了,可以打包了,现将阿里镜像配置信息如下所示:
<!--<mirror>
<id>mirrorId</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://central.maven.org/maven2/</url>
</mirror>-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!-- profiles