You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.*/packagecom.ximalaya.scala.common.logimportjava.io.{File, FileFilter, RandomAccessFile}
importjava.util.regex.Patternimportch.qos.logback.classic.{LoggerContext, Level⇒LogbackLevel}
importch.qos.logback.core.{FileAppender⇒LogbackAppender}
importorg.apache.commons.io.input.ReversedLinesFileReaderimportorg.apache.commons.io.FileUtilsimportorg.apache.commons.lang3.reflect.FieldUtilsimportorg.apache.log4j.{FileAppender⇒Log4jFileAppender, Level⇒Log4jLevel, Logger⇒Log4jLogger}
importorg.slf4j.LoggerFactoryimportorg.slf4j.impl.Log4jLoggerAdapterimportscala.collection.JavaConverters._/** * @author cjuexuan at 2018/5/7 15:59. * email : [email protected]*/objectLogUtilFactory {
//runtime set log levellazyvallogUtil= {
LoggerFactory.getILoggerFactory.getClass.getName match {
case"org.slf4j.impl.Log4jLoggerFactory"⇒newLog4JUtilcase"ch.qos.logback.classic.LoggerContext"⇒newLogbackUtil
}
}
}
traitLogUtil {
defsetLogLevel(logConfig: LogConfig):UnitdefgetLogLevel(className: String):StringvallogFile:FilelazyvaluserDirLogPath:String= {
valprojectDir=System.getProperty("user.dir")
newFile(projectDir).listFiles(newFileFilter {
privatevalpattern=Pattern.compile(".*\\.log")
overridedefaccept(pathname: File) = pattern.matcher(pathname.getName).matches()
}).headOption.map(_.getAbsolutePath).getOrElse("/tmp/log")
}
defheadLog(lines: Int=100):String= {
valrandomAccessFile=newRandomAccessFile(logFile, "r")
try {
valbuilder=StringBuilder.newBuilder
randomAccessFile.seek(0)
(1 to lines).takeWhile { _ ⇒valline= randomAccessFile.readLine()
if (line !=null) {
builder.append(line).append("\n")
true
} elsefalse
}
builder.toString()
} finally {
randomAccessFile.close()
}
}
deftailLog(lines: Int=100):String= {
valreversedLinesFileReader=newReversedLinesFileReader(logFile)
try {
valbuilder=StringBuilder.newBuilder
(1 to lines).takeWhile { _ ⇒Option(reversedLinesFileReader.readLine()).map { line ⇒
builder.append(line.reverse).append("\n")
}.isDefined
}
builder.reverse.mkString
} finally {
reversedLinesFileReader.close()
}
}
//TODO refinedeffullLog:String=FileUtils.readFileToString(logFile)
}
classLogbackUtilextendsLogUtil {
overridedefsetLogLevel(logConfig: LogConfig):Unit= {
getLogbackLogger(logConfig.fullClass)
.setLevel(LogbackLevel.valueOf(logConfig.logLevel))
}
overridedefgetLogLevel(className: String) = {
getLogbackLogger(className)
.getEffectiveLevel.toString.toUpperCase()
}
privatedefgetLogbackLogger(className: String) = {
LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext].getLogger(className)
}
overridevallogFile= {
vallogFileName=LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext]
.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME)
.iteratorForAppenders().asScala
.find(_.isInstanceOf[LogbackAppender[_]])
.map(_.asInstanceOf[LogbackAppender[_]].getFile).getOrElse(userDirLogPath)
newFile(logFileName)
}
}
classLog4JUtilextendsLogUtil {
overridedefsetLogLevel(logConfig: LogConfig):Unit= {
getLog4jLogger(logConfig.fullClass)
.setLevel(Log4jLevel.toLevel(logConfig.logLevel))
}
overridedefgetLogLevel(className: String) = {
getLog4jLogger(className).getEffectiveLevel.toString.toUpperCase()
}
privatedefgetLog4jLogger(className: String) = {
vallogger=LoggerFactory.getILoggerFactory.getLogger(className).asInstanceOf[Log4jLoggerAdapter]
FieldUtils.getAllFields(logger.getClass)
.filter(_.getName =="logger")
.map { field ⇒ field.setAccessible(true); field }
.head
.get(logger)
.asInstanceOf[Log4jLogger]
}
overridevallogFile= {
Log4jLogger.getRootLogger.getAllAppenders.asScala
.find(_.isInstanceOf[Log4jFileAppender]).map { appender ⇒newFile(appender.asInstanceOf[Log4jFileAppender].getFile)
}.getOrElse(newFile(userDirLogPath))
}
}
效果图
The text was updated successfully, but these errors were encountered:
平时工作中web服务日志的需求总结下大概有这么几种需求:
从这几个需求出发,我完善了下我们的日志类的工具,考虑到有些工程使用的是log4j,有些工程使用的是logback,所以这里选择用runtime的方式解决这两种需求的适配问题,另外由于log相关的配置实在是复杂,目前我涵盖的是我们常用的几种方式,比如指定了file输出的情况。另外,由于最外层的http server有很多,所以最后一步封装成http调用的代码我这里并没有给出,在我们内部,我们在akka-http-ext和spring-boot-ext中都实现了相关的逻辑,底层调用的是同一套代码。最后,由于file文件可能很大,所以目前给出的fulllog的实现其实不太好,最好还是用stream解决,不过由于平时看全部日志的需求并不是很多,所以找时间再优化下代码
效果图
The text was updated successfully, but these errors were encountered: