原文链接

一、日志管理

1.在application.properties文件中加入如下内容:

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.file=/log/log/my.log
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger- %msg%n

设置了日志输出的级别,以及生成日志文件。再简单一点的话,最后两行也可以不要。

2.使用slf4j日志框架:

private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
LOGGER.warn("您执行了UserController类中的userList2()方法。");

好了,不能写太多了。

二、配置Druid

1.在pom.xml文件中添加druid依赖:

        <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>

2.添加配置类,直接拷贝了别人的代码:

package com.yws710.springboot.demo1;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.HashMap;
import java.util.Map; @Configuration
public class DruidConfiguration { private static final Logger LOGGER = LoggerFactory.getLogger(DruidConfiguration.class); @Bean
public ServletRegistrationBean druidServlet() {
LOGGER.info("init Druid Servlet Configuration ");
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new StatViewServlet());
servletRegistrationBean.addUrlMappings("/druid/*");
Map<String, String> initParameters = new HashMap<String, String>();
initParameters.put("loginUsername", "admin");// 用户名
initParameters.put("loginPassword", "admin");// 密码
initParameters.put("resetEnable", "false");// 禁用HTML页面上的“Reset All”功能
initParameters.put("allow", ""); // IP白名单 (没有配置或者为空,则允许所有访问)
//initParameters.put("deny", "192.168.20.38");// IP黑名单 (存在共同时,deny优先于allow)
servletRegistrationBean.setInitParameters(initParameters);
return servletRegistrationBean;
} @Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}   @Bean
  @ConfigurationProperties(prefix="spring.datasource")
    public DataSource druidDataSource() {
    return new DruidDataSource();
  }
}

3.添加配置文件,打开application.properties文件,加入如下代码:

##Druid##
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo1?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456 spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.useGlobalDataSourceStat=true spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true

参考:https://github.com/alibaba/druid/

4.启动项目。在浏览器地址栏中输入http://localhost:8080/druid/

由于上面配置了用户名和密码,因此需要登录:

登录成功后的页面就不截图了,用过Druid的自然知道,没用过的赶紧用啊,这么好的东西。

Spring Boot入门第三天:配置日志系统和Druid数据库连接池。的更多相关文章

  1. Spring Boot入门第四天:使用Thymeleaf模板引擎

    原文链接 关于Thymeleaf的优点,我只说一条:它就是html页面啊,直接可以用浏览器打开.受够了JSP的同学可以尝试一下. 1.在pom.xml文件中添加依赖: <!--<depen ...

  2. spring boot 教程(三)配置详解

    在大部分情况下,我们不需要做太多的配置就能够让spring boot正常运行.在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性. Spring Boot 支持多种外部配置方式 这些 ...

  3. Spring Boot入门第五天:使用JSP

    原文链接 1.在pom.xml文件中添加依赖: <!-- Servlet依赖 --> <dependency> <groupId>javax.servlet< ...

  4. spring boot:配置druid数据库连接池(开启sql防火墙/使用log4j2做异步日志/spring boot 2.3.2)

    一,druid数据库连接池的功能? 1,Druid是阿里巴巴开发的号称为监控而生的数据库连接池 它的优点包括: 可以监控数据库访问性能 SQL执行日志 SQL防火墙 2,druid的官方站: http ...

  5. spring boot 笔记--第三章

    spring boot 笔记 第三章,使用Spring boot 构建系统: 强烈建议支持依赖管理的构建系统,Maven或Gradle 依赖管理: Spring Boot的每版本都会提供它支持的依赖列 ...

  6. Spring Boot 2.X(十四):日志功能 Logback

    Logback 简介 Logback 是由 SLF4J 作者开发的新一代日志框架,用于替代 log4j. 主要特点是效率更高,架构设计够通用,适用于不同的环境. Logback 分为三个模块:logb ...

  7. Spring Boot应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

  8. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  9. JAR(Spring Boot)应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

随机推荐

  1. Linux普通用户不能使用TAB键、上下键

    出发点 今天安装使用kail linux的时候发现tab键命令不能补全, 结合ubuntu, 因默认ubuntu创建的普通帐号,默认shell为/bin/sh,而这不支持tab等键的,所以将「指定用户 ...

  2. bzoj 3237 连通图 - 并查集 - 线段树

    Input Output Sample Input 4 5 1 2 2 3 3 4 4 1 2 4 3 1 5 2 2 3 2 1 2 Sample Output Connected Disconne ...

  3. Flutter提升开发效率的一些方法和工具

    Flutter的环境搭配完之后,就开始Flutter的开发,下面的一些工具和方法,可以省下一些时间. 自己在用的,暂时想到的,就是这些了,总结一下. 1.JSON解析快速生成实体类 根据接口返回的数据 ...

  4. 洛谷1968美元汇率 dp

    P1968 美元汇率 dp 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后能获得最高可能的价值. 输入输出格式 输入格式: ...

  5. Docker 使用Dockerfile构建tomcat镜像

    Dockerfile概念: 镜像的定制实际上就是定制每一层所添加的配置.文件.如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么之前提及的无法重复的问题. ...

  6. vue中使用BetterScroll

    BetterScroll可以通过给content加min-height实现永远滚动 content千万不可以删除,千万不要在 content上写v-if

  7. Kubernetes工作流之Pods二

    Init Containers This feature has exited beta in 1.6. Init Containers can be specified in the PodSpec ...

  8. 6、lvs使用进阶(02)

    把web server服务和443服务绑定在一起之后呢? 假设一种场景,对web服务器来讲需要session保持.一个在线购物网站,在购物时,如果不结账,一般是http协议,当结账时,需要网站跳转,可 ...

  9. JQGrid 在页面加载时展开SubGrid

    1.jqgrid在加载完成之后展开SubGrid,用得如下办法: gridComplete: function () { var rowIds = $("#list2").getD ...

  10. JS加载获取父窗体传递的参数

    JS加载获取父窗体传递的参数 $(document).ready(function () { var query = location.search.substring(1); var values ...