Skip to content
This repository has been archived by the owner on Dec 28, 2019. It is now read-only.

Commit

Permalink
Added file logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tammo0987 committed May 18, 2019
1 parent efabd2d commit f13edd0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ out
*.ipr
*.iws
*.iml


# Fiber
working/
1 change: 1 addition & 0 deletions Fiber-Node/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {

implementation(Dependencies.joptSimple)
implementation(Dependencies.koin)
implementation(Dependencies.zip4j)
}

this.tasks.withType<ShadowJar> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.fiber.project.node.logging

import app.fiber.project.node.logging.file.FileLogger
import java.time.LocalTime
import java.time.format.DateTimeFormatter

class Logger {

private val fileLogger = FileLogger()

fun debug(message: String) = this.print(LogLevel.DEBUG, message)

fun info(message: String, newLine: Boolean = true) = this.print(LogLevel.INFO, message, newLine)
Expand All @@ -14,7 +17,9 @@ class Logger {
fun error(message: String) = this.print(LogLevel.ERROR, message)

private fun print(level: LogLevel, message: String, newLine: Boolean = true) {
println(this.formatLog(level, message))
val log = this.formatLog(level, message)
println(log)
this.fileLogger.log(log)
if(newLine) this.printPrefix()
}

Expand All @@ -25,6 +30,4 @@ class Logger {

private fun printPrefix() = print("\r>")

//TODO files

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package app.fiber.project.node.logging.file

import net.lingala.zip4j.core.ZipFile
import net.lingala.zip4j.model.ZipParameters
import net.lingala.zip4j.util.Zip4jConstants
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class FileLogger {

private val logDirectory = Paths.get("logs")

private val latest = Paths.get("logs/latest.log")

init {
if (Files.exists(this.latest)) {
this.archiveLatestLog()
Files.delete(this.latest)
}
}

fun log(message: String) {
if (Files.notExists(this.logDirectory)) Files.createDirectories(this.logDirectory)

if(Files.notExists(this.latest)) Files.createFile(this.latest)

Files.newBufferedWriter(this.latest, StandardOpenOption.APPEND).use {
it.write(message)
}
}

fun clearLog() = this.logDirectory.toFile().listFiles().forEach { Files.delete(it.toPath()) }

private fun archiveLatestLog() = ZipFile(this.nameForZip()).addFile(this.latest.toFile(), ZipParameters().apply {
this.compressionMethod = Zip4jConstants.COMP_DEFLATE
this.compressionLevel = Zip4jConstants.DEFLATE_LEVEL_NORMAL
})

private fun nameForZip(): String {
val millis = Files.getLastModifiedTime(this.latest).toMillis()
val date = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault()).toLocalDateTime()
return "logs/${DateTimeFormatter.ofPattern("yyyy-MM-dd--HH-mm").format(date)}.log.zip"
}

}
6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ object Versions {
//Jackson
const val jackson = "2.9.+"

//Zip4J
const val zip4j = "1.3.3"

}

object Dependencies {
Expand All @@ -48,6 +51,9 @@ object Dependencies {
const val jacksonKotlin = "com.fasterxml.jackson.module:jackson-module-kotlin:${Versions.jackson}"
const val jacksonYaml = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${Versions.jackson}"

//Zip4J
const val zip4j = "net.lingala.zip4j:zip4j:${Versions.zip4j}"

}

object Plugins {
Expand Down

0 comments on commit f13edd0

Please sign in to comment.