Skip to content

Commit

Permalink
(refs #25)Log directory and file became configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Jan 7, 2017
1 parent 1300a75 commit e4b164f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,25 @@ Add following parameters to `web.xml` to enable Zipkin integration:
</context-param>
```

## Dynamic LogBack configuration
## Log console

You can change log level of LogBack loggers dynamically via Web UI at `http://localhost:8080/logger-ui/`.
Resty provides a log console at `http://localhost:8080/logger-ui/`. You can change log level of LogBack loggers dynamically, download log files and tail log file on this console.

![Dynamic LogBack configuration](dynamic-logback.png)

Add a following parameter to `web.xml` to enable dynamic LogBack configuration:

```xml
<context-param>
<param-name>resty.dynamic-logback</param-name>
<param-name>resty.logconsole</param-name>
<param-value>enable</param-value>
</context-param>
<context-param>
<param-name>resty.logconsole.dir</param-name>
<param-value>logs</param-value>
</context-param>
<context-param>
<param-name>resty.logconsole.file</param-name>
<param-value>application.log</param-value>
</context-param>
```
4 changes: 2 additions & 2 deletions src/main/resources/public/resty/logger-ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<html>
<head>
<meta charset="UTF-8">
<title>LogBack Configuration</title>
<title>Log console</title>
<link href='bootstrap-3.3.7/css/bootstrap.min.css' rel='stylesheet' type='text/css'/>
<script src='jquery-3.1.1/jquery-3.1.1.min.js' type='text/javascript'></script>
<script src='bootstrap-3.3.7/js/bootstrap.min.js' type='text/javascript'></script>
<script src="vuejs-2.1.7/vue.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container" id="app">
<h1 class="page-header">LogBack Configuration</h1>
<h1 class="page-header">Log console</h1>
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#level" aria-controls="level" role="tab" data-toggle="tab">Log level</a></li>
<li role="presentation"><a href="#download" aria-controls="download" role="tab" data-toggle="tab">Download</a></li>
Expand Down
11 changes: 5 additions & 6 deletions src/main/scala/com/github/takezoe/resty/LoggerController.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.github.takezoe.resty

import java.nio.file.{Files, Path, Paths}
import java.util.concurrent.atomic.AtomicBoolean
import java.nio.file.{Files, Paths}

import ch.qos.logback.classic.{Level, LoggerContext}
import org.slf4j.impl.StaticLoggerBinder

import scala.collection.JavaConverters._

class LoggerController {
class LoggerController(dir: String, file: String) {

@Action(method = "GET", path = "/logger/levels")
def levels(): Seq[LoggerModel] = {
Expand Down Expand Up @@ -47,7 +46,7 @@ class LoggerController {

@Action(method = "GET", path = "/logger/files")
def getLogFiles(): Array[LogFileModel] = {
Files.list(Paths.get("logs")).map[LogFileModel] { file =>
Files.list(Paths.get(dir)).map[LogFileModel] { file =>
LogFileModel(file.getFileName.toString, Files.size(file))
}.toArray { length =>
new Array[LogFileModel](length)
Expand All @@ -56,15 +55,15 @@ class LoggerController {

@Action(method = "GET", path = "/logger/files/{name}")
def downloadLogFile(name: String): java.io.File = {
Paths.get("logs", name).toFile match {
Paths.get(dir, name).toFile match {
case file if !file.exists => NotFound()
case file => file
}
}

@Action(method = "GET", path = "/logger/tail")
def tailLogFile(from: Int): Seq[String] = {
val path = Paths.get("logs", "application.log")
val path = Paths.get(dir, file)
if(Files.exists(path)){
val stream = Files.lines(Paths.get("logs", "application.log"))
stream.skip(from).toArray { length => new Array[String](length) }.toSeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class InitializeListener extends ServletContextListener {
}

// Initialize LogBack dynamic configuration
if("enable" == StringUtils.trim(context.getInitParameter(ConfigKeys.DynamicLogBack))){
Resty.register(new LoggerController())
if("enable" == StringUtils.trim(context.getInitParameter(ConfigKeys.LogConsole))){
val dir = StringUtils.trim(context.getInitParameter(ConfigKeys.LogConsoleDir))
val file = StringUtils.trim(context.getInitParameter(ConfigKeys.LogConsoleFile))
Resty.register(new LoggerController(dir, file))
context.addServlet("LoggerUIServlet", new LoggerUIServlet())
context.getServletRegistration("LoggerUIServlet").addMapping("/logger-ui/*")
}
Expand All @@ -57,5 +59,7 @@ object ConfigKeys {
val ZipkinSampleRate = "resty.zipkin.sample.rate"
val SwaggerSupport = "resty.swagger"
val HystrixSupport = "resty.hystrix"
val DynamicLogBack = "resty.dynamic-logback"
val LogConsole = "resty.logconsole"
val LogConsoleDir = "resty.logconsole.dir"
val LogConsoleFile = "resty.logconsole.file"
}

0 comments on commit e4b164f

Please sign in to comment.