Permalink
Browse files

adding max_file_size_bytes logging param

  • Loading branch information...
1 parent 1378f4a commit bf48fa78344152d403bc2bcce4c5c490d6683f69 Jamie Paulson committed Jun 16, 2011
View
16 src/main/scala/net/lag/logging/FileHandler.scala
@@ -26,7 +26,7 @@ case object Never extends Policy
case object Hourly extends Policy
case object Daily extends Policy
case class Weekly(dayOfWeek: Int) extends Policy
-
+case class MaxSize(sizeBytes: Long) extends Policy
/**
* A log handler that writes log entries into a file, and rolls this file
@@ -84,6 +84,7 @@ class FileHandler(val filename: String, val policy: Policy, formatter: Formatter
case Hourly => new SimpleDateFormat("yyyyMMdd-HH")
case Daily => new SimpleDateFormat("yyyyMMdd")
case Weekly(_) => new SimpleDateFormat("yyyyMMdd")
+ case MaxSize(_) => new SimpleDateFormat("yyyyMMdd-hhmmss")
}
dateFormat.setCalendar(formatter.calendar)
dateFormat.format(date)
@@ -100,6 +101,8 @@ class FileHandler(val filename: String, val policy: Policy, formatter: Formatter
next.set(Calendar.SECOND, 0)
next.set(Calendar.MINUTE, 0)
policy match {
+ case MaxSize(_) =>
+ next.add(Calendar.YEAR, 100)
case Never =>
next.add(Calendar.YEAR, 100)
case Hourly =>
@@ -117,6 +120,11 @@ class FileHandler(val filename: String, val policy: Policy, formatter: Formatter
}
def computeNextRollTime(): Long = computeNextRollTime(System.currentTimeMillis)
+
+ def maxFileSizeBytes = policy match {
+ case MaxSize(sizeBytes) => sizeBytes
+ case _ => Long.MaxValue
+ }
/**
* Delete files when "too many" have accumulated.
@@ -155,6 +163,12 @@ class FileHandler(val filename: String, val policy: Policy, formatter: Formatter
if (System.currentTimeMillis > nextRollTime) {
roll
}
+ if (maxFileSizeBytes != Long.MaxValue) {
+ if (new File(filename).length + formattedLine.getBytes("UTF-8").length > maxFileSizeBytes) {
+ println("ROLL!")
+ roll
+ }
+ }
stream.write(formattedLine)
stream.flush
}
View
9 src/main/scala/net/lag/logging/Logger.scala
@@ -348,7 +348,8 @@ object Logger {
"scribe_buffer_msec", "scribe_backoff_msec",
"scribe_max_packet_size", "scribe_category",
"throttle_period_msec", "throttle_rate", "handle_sighup",
- "scribe_max_buffer", "syslog_priority", "rotate_count")
+ "scribe_max_buffer", "syslog_priority", "rotate_count",
+ "max_file_size_bytes")
var forbidden = config.keys.filter(x => !(allowed contains x)).toList
if (allowNestedBlocks) {
forbidden = forbidden.filter(x => !config.getConfigMap(x).isDefined)
@@ -387,7 +388,7 @@ object Logger {
for (filename <- config.getString("filename")) {
var handleSighup = config.getBool("handle_sighup", false)
- val policy = config.getString("roll", "never").toLowerCase match {
+ val timeBasedPolicy = config.getString("roll", "never").toLowerCase match {
case "never" => Never
case "hourly" => Hourly
case "daily" => Daily
@@ -401,6 +402,10 @@ object Logger {
case "hup" => handleSighup = true; Never
case x => throw new LoggingException("Unknown logfile rolling policy: " + x)
}
+ val policy = config.getLong("max_file_size_bytes") match {
+ case Some(size) => MaxSize(size)
+ case None => timeBasedPolicy
+ }
val handler =
new FileHandler(filename, policy, formatter, config.getBool("append", true), handleSighup)
val rotateCount = config.getInt("rotate_count", -1 )
View
34 src/test/scala/net/lag/logging/LoggingSpec.scala
@@ -569,5 +569,39 @@ object LoggingSpec extends Specification with TestHelper {
log1.getHandlers()(1) must haveClass[ScribeHandler]
log1.getHandlers()(1).getLevel mustEqual Logger.FATAL
}
+
+ "rotate log files based on max size" in {
+ withTempFolder {
+ // assuming log prefix of 37 chars, e.g. "FAT [20110615-21:23:08.608] whiskey: "
+ // 90 gives us space for two 7 char log lines (incl endline)
+ val maxSize = 90
+
+ new File(folderName).list().length mustEqual 0
+
+ val log = Logger.get("net.lag.whiskey.Train")
+ val handler = new FileHandler(folderName + "/test.log", MaxSize(maxSize), new FileFormatter, true, true)
+ log.addHandler(handler)
+
+ log.fatal("log one")
+ Thread.sleep(500)
+ new File(folderName).list.length mustEqual 1
+
+ log.fatal("log two") // should just fit into max size of 90 including prefixes and newlines...
+ Thread.sleep(500)
+ new File(folderName).list.length mustEqual 1
+
+ log.fatal("log three") // should roll since first file is full
+ Thread.sleep(500)
+ new File(folderName).list.length mustEqual 2
+
+ log.fatal("log four") // shouldn't fit, another causing a roll
+ Thread.sleep(500)
+ new File(folderName).list.length mustEqual 3
+
+ new File(folderName).list.toList.foreach (f => f.length must beLessThan (maxSize))
+
+ handler.close()
+ }
+ }
}
}

0 comments on commit bf48fa7

Please sign in to comment.