我们之前在启动项目的时候,会有log4j的警告,那是因为我们没有加入日志信息。
加入日志信息我们需要进行下面操作:
1、首先在resources的根目录创建log4j.properties文件
### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n ### 输出DEBUG 级别以上的日志到=E:/Users/allen/text/logs/log.log ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = /Users/allen/test/logs/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 输出ERROR 级别以上的日志到=/Users/allen/text/logs/error.log ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =/Users/allen/test/logs/error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
2、声明日志
final InternalLogger logger = Log4JLoggerFactory.getInstance(StaticFileVerticle.class);
3、使用日志
logger.error("进来了啊啊啊");
4、例子StaticFileVerticle
package vertx; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.Log4JLoggerFactory; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.StaticHandler; import io.vertx.ext.web.templ.thymeleaf.ThymeleafTemplateEngine; public class StaticFileVerticle extends AbstractVerticle { final InternalLogger logger = Log4JLoggerFactory.getInstance(StaticFileVerticle.class); //声明Router Router router; //声明ThymeleafTemplateEngine ThymeleafTemplateEngine thymeleafTemplateEngine; @Override public void start(Promise<Void> startPromise) throws Exception { //初始化Router router = Router.router(vertx); //初始化ThymeleafTemplateEngine thymeleafTemplateEngine = ThymeleafTemplateEngine.create(vertx); //整合静态资源 第一步 router.route("/static/*").handler(StaticHandler.create()); //整合静态资源 自定义访问路径 // router.route("/*").handler(StaticHandler.create()); //配置Router解析url router.route("/").handler( req -> { logger.error("进来了啊啊啊"); var obj = new JsonObject(); obj.put("name", "Hello World from backend"); //ThymeleafTemplateEngine 直接 render thymeleafTemplateEngine.render(obj, "templates/index.html", bufferAsyncResult -> { if (bufferAsyncResult.succeeded()){ req.response() .putHeader("content-type", "text/html") .end(bufferAsyncResult.result()); }else{ System.out.println("error"); } }); } ); //将Router与vertx HttpServer 绑定 vertx.createHttpServer().requestHandler(router).listen(8888, http -> { if (http.succeeded()) { startPromise.complete(); System.out.println("HTTP server started on port 8888"); } else { startPromise.fail(http.cause()); } }); } }
5、测试
启动
调用接口