001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.actuate.logging; 018 019import java.io.File; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023 024import org.springframework.boot.actuate.endpoint.annotation.Endpoint; 025import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; 026import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint; 027import org.springframework.boot.logging.LogFile; 028import org.springframework.core.env.Environment; 029import org.springframework.core.io.FileSystemResource; 030import org.springframework.core.io.Resource; 031 032/** 033 * Web {@link Endpoint} that provides access to an application's log file. 034 * 035 * @author Johannes Edmeier 036 * @author Phillip Webb 037 * @author Andy Wilkinson 038 * @since 2.0.0 039 */ 040@WebEndpoint(id = "logfile") 041public class LogFileWebEndpoint { 042 043 private static final Log logger = LogFactory.getLog(LogFileWebEndpoint.class); 044 045 private final Environment environment; 046 047 private File externalFile; 048 049 public LogFileWebEndpoint(Environment environment, File externalFile) { 050 this.environment = environment; 051 this.externalFile = externalFile; 052 } 053 054 public LogFileWebEndpoint(Environment environment) { 055 this(environment, null); 056 } 057 058 @ReadOperation 059 public Resource logFile() { 060 Resource logFileResource = getLogFileResource(); 061 if (logFileResource == null || !logFileResource.isReadable()) { 062 return null; 063 } 064 return logFileResource; 065 } 066 067 private Resource getLogFileResource() { 068 if (this.externalFile != null) { 069 return new FileSystemResource(this.externalFile); 070 } 071 LogFile logFile = LogFile.get(this.environment); 072 if (logFile == null) { 073 logger.debug("Missing 'logging.file' or 'logging.path' properties"); 074 return null; 075 } 076 return new FileSystemResource(logFile.toString()); 077 } 078 079}