Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-boot-admin
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-boot-admin
Commits
190e6280
Commit
190e6280
authored
Nov 16, 2014
by
Johannes Stelzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
publish logfile via actuate endpoint not via controller
parent
7b1e6906
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
48 deletions
+118
-48
LogfileMvcEndpoint.java
...de/codecentric/boot/admin/actuate/LogfileMvcEndpoint.java
+80
-31
SpringBootAdminClientAutoConfiguration.java
.../admin/config/SpringBootAdminClientAutoConfiguration.java
+9
-5
LogfileMvcEndpointTest.java
...odecentric/boot/admin/actuate/LogfileMvcEndpointTest.java
+29
-12
No files found.
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/
controller/LogfileController
.java
→
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/
actuate/LogfileMvcEndpoint
.java
View file @
190e6280
...
...
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
de
.
codecentric
.
boot
.
admin
.
controller
;
package
de
.
codecentric
.
boot
.
admin
.
actuate
;
import
java.io.IOException
;
...
...
@@ -22,6 +22,9 @@ import javax.servlet.http.HttpServletResponse;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.actuate.endpoint.Endpoint
;
import
org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.core.io.FileSystemResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -30,62 +33,108 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.Res
tController
;
import
org.springframework.web.bind.annotation.Res
ponseBody
;
/**
* Controller that provides an API for logfiles, i.e. downloading the main logfile configured in environment property
* 'logging.file' that is standard, but optional property for spring-boot applications.
*/
@
RestController
public
class
Logfile
Controller
{
@
ConfigurationProperties
(
prefix
=
"endpoints.logfile"
)
public
class
Logfile
MvcEndpoint
implements
MvcEndpoint
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
Logfile
Controller
.
class
);
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
Logfile
MvcEndpoint
.
class
);
@Value
(
"${logging.file}"
)
private
String
logfile
;
@RequestMapping
(
value
=
"/logfile"
,
method
=
{
RequestMethod
.
GET
})
public
String
getLogfile
(
HttpServletResponse
response
)
{
if
(
logfile
==
null
)
{
LOGGER
.
error
(
"Logfile download failed for missing property 'logging.file'"
);
response
.
setStatus
(
HttpStatus
.
NOT_FOUND
.
value
());
return
"Logfile download failed for missing property 'logging.file'"
;
private
String
path
=
"/logfile"
;
private
boolean
sensitive
=
true
;
private
boolean
enabled
=
true
;
@Override
public
boolean
isSensitive
()
{
return
sensitive
;
}
Resource
file
=
new
FileSystemResource
(
logfile
);
if
(!
file
.
exists
())
{
LOGGER
.
error
(
"Logfile download failed for missing file at path={}"
,
logfile
);
@Override
public
String
getPath
()
{
return
path
;
}
@Override
@SuppressWarnings
(
"rawtypes"
)
public
Class
<?
extends
Endpoint
>
getEndpointType
()
{
return
null
;
}
public
void
setLogfile
(
String
logfile
)
{
this
.
logfile
=
logfile
;
}
public
String
getLogfile
()
{
return
logfile
;
}
public
void
setPath
(
String
path
)
{
this
.
path
=
path
;
}
public
void
setSensitive
(
boolean
sensitive
)
{
this
.
sensitive
=
sensitive
;
}
public
void
setEnabled
(
boolean
enabled
)
{
this
.
enabled
=
enabled
;
}
public
boolean
isEnabled
()
{
return
enabled
;
}
@RequestMapping
(
method
=
RequestMethod
.
GET
)
public
void
invoke
(
HttpServletResponse
response
)
throws
IOException
{
if
(!
isAvailable
())
{
response
.
setStatus
(
HttpStatus
.
NOT_FOUND
.
value
());
return
"Logfile download failed for missing file at path="
+
logfile
;
return
;
}
response
.
setContentType
(
MediaType
.
TEXT_PLAIN_VALUE
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename=\""
+
file
.
getFilename
()
+
"\""
);
try
{
Resource
file
=
new
FileSystemResource
(
logfile
);
response
.
setContentType
(
MediaType
.
TEXT_PLAIN_VALUE
);
FileCopyUtils
.
copy
(
file
.
getInputStream
(),
response
.
getOutputStream
());
}
catch
(
IOException
ex
)
{
LOGGER
.
error
(
"Logfile download failed for path={}. Reasond: {}"
,
logfile
,
ex
.
getMessage
());
response
.
setStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
.
value
());
return
"Logfile download failed for path="
+
logfile
;
}
return
null
;
@RequestMapping
(
method
=
RequestMethod
.
HEAD
)
@ResponseBody
public
ResponseEntity
<
Void
>
available
()
{
if
(
isAvailable
())
{
return
new
ResponseEntity
<
Void
>(
HttpStatus
.
OK
);
}
else
{
return
new
ResponseEntity
<
Void
>(
HttpStatus
.
NOT_FOUND
);
}
}
private
boolean
isAvailable
()
{
if
(!
enabled
)
{
return
false
;
}
@RequestMapping
(
value
=
"/logfile"
,
method
=
{
RequestMethod
.
HEAD
})
public
ResponseEntity
<?>
hasLogfile
()
{
if
(
logfile
==
null
)
{
return
new
ResponseEntity
<
Void
>(
HttpStatus
.
NOT_FOUND
);
LOGGER
.
error
(
"Logfile download failed for missing property 'logging.file'"
);
return
false
;
}
Resource
file
=
new
FileSystemResource
(
logfile
);
if
(!
file
.
exists
())
{
return
new
ResponseEntity
<
Void
>(
HttpStatus
.
NOT_FOUND
);
LOGGER
.
error
(
"Logfile download failed for missing file at path={}"
,
logfile
);
return
false
;
}
return
new
ResponseEntity
<
Void
>(
HttpStatus
.
OK
)
;
return
true
;
}
public
void
setLogfile
(
String
logfile
)
{
this
.
logfile
=
logfile
;
}
}
spring-boot-starter-admin-client/src/main/java/de/codecentric/boot/admin/config/SpringBootAdminClientAutoConfiguration.java
View file @
190e6280
...
...
@@ -15,6 +15,7 @@
*/
package
de
.
codecentric
.
boot
.
admin
.
config
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
...
...
@@ -23,7 +24,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
import
org.springframework.scheduling.config.ScheduledTaskRegistrar
;
import
org.springframework.web.client.RestTemplate
;
import
de.codecentric.boot.admin.
controller.LogfileController
;
import
de.codecentric.boot.admin.
actuate.LogfileMvcEndpoint
;
import
de.codecentric.boot.admin.services.SpringBootAdminRegistrator
;
import
de.codecentric.boot.admin.web.SimpleCORSFilter
;
...
...
@@ -79,13 +80,16 @@ public class SpringBootAdminClientAutoConfiguration {
return
registrar
;
}
@Configuration
@ConditionalOnExpression
(
"${endpoints.logfile.enabled:true}"
)
public
static
class
LogfileEndpointAutoConfiguration
{
/**
* Controller to do something with the application logfile(s).
* Exposes the logfile as acutator endpoint
*/
@Bean
@ConditionalOnProperty
(
"logging.file"
)
public
LogfileController
logfileController
()
{
return
new
LogfileController
();
public
LogfileMvcEndpoint
logfileEndpoint
()
{
return
new
LogfileMvcEndpoint
();
}
}
}
spring-boot-starter-admin-client/src/test/java/de/codecentric/boot/admin/
controller/LogfileController
Test.java
→
spring-boot-starter-admin-client/src/test/java/de/codecentric/boot/admin/
actuate/LogfileMvcEndpoint
Test.java
View file @
190e6280
package
de
.
codecentric
.
boot
.
admin
.
controller
;
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
de
.
codecentric
.
boot
.
admin
.
actuate
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -10,27 +25,28 @@ import org.springframework.http.HttpStatus;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.util.FileCopyUtils
;
public
class
LogfileControllerTest
{
private
LogfileController
controller
=
new
LogfileController
();
public
class
LogfileMvcEndpointTest
{
private
LogfileMvcEndpoint
controller
=
new
LogfileMvcEndpoint
();
@Test
public
void
logfile_noProperty
()
{
assertEquals
(
HttpStatus
.
NOT_FOUND
,
controller
.
hasLogfi
le
().
getStatusCode
());
public
void
logfile_noProperty
()
throws
IOException
{
assertEquals
(
HttpStatus
.
NOT_FOUND
,
controller
.
availab
le
().
getStatusCode
());
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
controller
.
getLogfil
e
(
response
);
controller
.
invok
e
(
response
);
assertEquals
(
HttpStatus
.
NOT_FOUND
.
value
(),
response
.
getStatus
());
}
@Test
public
void
logfile_noFile
()
{
public
void
logfile_noFile
()
throws
IOException
{
controller
.
setLogfile
(
"does_not_exist.log"
);
assertEquals
(
HttpStatus
.
NOT_FOUND
,
controller
.
hasLogfi
le
().
getStatusCode
());
assertEquals
(
HttpStatus
.
NOT_FOUND
,
controller
.
availab
le
().
getStatusCode
());
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
controller
.
getLogfil
e
(
response
);
controller
.
invok
e
(
response
);
assertEquals
(
HttpStatus
.
NOT_FOUND
.
value
(),
response
.
getStatus
());
}
...
...
@@ -40,13 +56,14 @@ public class LogfileControllerTest {
FileCopyUtils
.
copy
(
"--TEST--"
.
getBytes
(),
new
File
(
"test.log"
));
controller
.
setLogfile
(
"test.log"
);
assertEquals
(
HttpStatus
.
OK
,
controller
.
hasLogfi
le
().
getStatusCode
());
assertEquals
(
HttpStatus
.
OK
,
controller
.
availab
le
().
getStatusCode
());
MockHttpServletResponse
response
=
new
MockHttpServletResponse
();
controller
.
getLogfil
e
(
response
);
controller
.
invok
e
(
response
);
assertEquals
(
HttpStatus
.
OK
.
value
(),
response
.
getStatus
());
assertEquals
(
"--TEST--"
,
response
.
getContentAsString
());
}
finally
{
}
finally
{
new
File
(
"test.log"
).
delete
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment