From 11cbcb23128f64789ecfe11a1f8c613e598be6bf Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 11 May 2019 20:18:55 +0100 Subject: [PATCH] Use logging in place of println --- README.org | 2 +- src/main/scala/net/kemitix/s3thorp/Logging.scala | 7 +++++++ src/main/scala/net/kemitix/s3thorp/Main.scala | 12 +++++------- .../net/kemitix/s3thorp/S3MetaDataEnricher.scala | 7 +++++-- src/main/scala/net/kemitix/s3thorp/S3Uploader.scala | 8 ++++---- src/main/scala/net/kemitix/s3thorp/Sync.scala | 5 +++-- .../net/kemitix/s3thorp/UploadSelectionFilter.scala | 7 ++++--- 7 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 src/main/scala/net/kemitix/s3thorp/Logging.scala diff --git a/README.org b/README.org index 32c3371..2fb0689 100644 --- a/README.org +++ b/README.org @@ -23,7 +23,7 @@ Usage: S3Thorp [options] - [x] Improve test coverage - [ ] Create os-native binaries -- [ ] Replace println with real logging +- [x] Replace println with real logging - [ ] Add support for logging options - [ ] Add support for exclusion filters - [ ] Bulk fetching of Hash values from S3 diff --git a/src/main/scala/net/kemitix/s3thorp/Logging.scala b/src/main/scala/net/kemitix/s3thorp/Logging.scala new file mode 100644 index 0000000..5689c12 --- /dev/null +++ b/src/main/scala/net/kemitix/s3thorp/Logging.scala @@ -0,0 +1,7 @@ +package net.kemitix.s3thorp + +import com.typesafe.scalalogging.LazyLogging + +trait Logging extends LazyLogging { + +} diff --git a/src/main/scala/net/kemitix/s3thorp/Main.scala b/src/main/scala/net/kemitix/s3thorp/Main.scala index d7bf9a7..98cac12 100644 --- a/src/main/scala/net/kemitix/s3thorp/Main.scala +++ b/src/main/scala/net/kemitix/s3thorp/Main.scala @@ -6,9 +6,7 @@ import cats.effect.ExitCase.{Canceled, Completed, Error} import cats.effect.{ExitCode, IO, IOApp} import net.kemitix.s3thorp.awssdk.S3Client -object Main extends IOApp { - - def putStrLn(value: String) = IO { println(value) } +object Main extends IOApp with Logging { val defaultConfig: Config = Config("(none)", "", Paths.get(".").toFile) @@ -17,7 +15,7 @@ object Main extends IOApp { def program(args: List[String]): IO[ExitCode] = for { - _ <- putStrLn("S3Thorp - hashed sync for s3") + _ <- IO(logger.info("S3Thorp - hashed sync for s3")) a <- ParseArgs(args, defaultConfig) _ <- sync.run(a) } yield ExitCode.Success @@ -25,9 +23,9 @@ object Main extends IOApp { override def run(args: List[String]): IO[ExitCode] = program(args) .guaranteeCase { - case Canceled => IO(println("Interrupted")) - case Error(e) => IO(println("ERROR: " + e.getMessage)) - case Completed => IO(println("Done")) + case Canceled => IO(logger.warn("Interrupted")) + case Error(e) => IO(logger.error(e.getMessage)) + case Completed => IO(logger.info("Done")) } } diff --git a/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala b/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala index f92fdad..43319a1 100644 --- a/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala +++ b/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala @@ -6,13 +6,16 @@ import fs2.Stream import cats.effect.IO import net.kemitix.s3thorp.awssdk.S3Client -trait S3MetaDataEnricher extends S3Client with KeyGenerator { +trait S3MetaDataEnricher + extends S3Client + with KeyGenerator + with Logging { def enrichWithS3MetaData(c: Config): File => Stream[IO, Either[File, S3MetaData]] = { val remoteKey = generateKey(c)_ file => Stream.eval({ - println(s"- Consider: ${c.relativePath(file)}") + logger.info(s"- Consider: ${c.relativePath(file)}") val key = remoteKey(file) for { head <- objectHead(c.bucket, key) diff --git a/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala b/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala index d7d817b..633fc66 100644 --- a/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala +++ b/src/main/scala/net/kemitix/s3thorp/S3Uploader.scala @@ -4,12 +4,12 @@ import java.io.File import fs2.Stream import cats.effect.IO -import net.kemitix.s3thorp.Main.putStrLn import net.kemitix.s3thorp.awssdk.S3Client trait S3Uploader extends S3Client - with KeyGenerator { + with KeyGenerator + with Logging { def performUpload(c: Config): File => Stream[IO, Unit] = { val remoteKey = generateKey(c) _ @@ -17,9 +17,9 @@ trait S3Uploader val key = remoteKey(file) val shortFile = c.relativePath(file) Stream.eval(for { - _ <- putStrLn(s" Upload: $shortFile") + _ <- IO(logger.info(s" Upload: $shortFile")) _ <- upload(file, c.bucket, key) - _ <- putStrLn(s" Done: $shortFile") + _ <- IO(logger.info(s" Done: $shortFile")) } yield ()) } } diff --git a/src/main/scala/net/kemitix/s3thorp/Sync.scala b/src/main/scala/net/kemitix/s3thorp/Sync.scala index 2e4ed8a..2ed910f 100644 --- a/src/main/scala/net/kemitix/s3thorp/Sync.scala +++ b/src/main/scala/net/kemitix/s3thorp/Sync.scala @@ -11,7 +11,8 @@ class Sync(s3Client: S3Client) extends LocalFileStream with S3MetaDataEnricher with UploadSelectionFilter - with S3Uploader { + with S3Uploader + with Logging { override def objectHead(bucket: Bucket, remoteKey: RemoteKey)= s3Client.objectHead(bucket, remoteKey) @@ -20,7 +21,7 @@ class Sync(s3Client: S3Client) s3Client.upload(localFile, bucket, remoteKey) def run(c: Config): IO[Unit] = { - println(s"Bucket: ${c.bucket}, Prefix: ${c.prefix}, Source: ${c.source}") + logger.info(s"Bucket: ${c.bucket}, Prefix: ${c.prefix}, Source: ${c.source}") streamDirectoryPaths(c.source).flatMap( enrichWithS3MetaData(c)).flatMap( uploadRequiredFilter(c)).flatMap( diff --git a/src/main/scala/net/kemitix/s3thorp/UploadSelectionFilter.scala b/src/main/scala/net/kemitix/s3thorp/UploadSelectionFilter.scala index 67c3d7b..4c75913 100644 --- a/src/main/scala/net/kemitix/s3thorp/UploadSelectionFilter.scala +++ b/src/main/scala/net/kemitix/s3thorp/UploadSelectionFilter.scala @@ -6,7 +6,8 @@ import net.kemitix.s3thorp.Sync.{MD5Hash, LocalFile} import java.security.{MessageDigest, DigestInputStream} import java.io.{File, FileInputStream} -trait UploadSelectionFilter { +trait UploadSelectionFilter + extends Logging { private def md5File(localFile: LocalFile): MD5Hash = { val buffer = new Array[Byte](8192) @@ -20,7 +21,7 @@ trait UploadSelectionFilter { def uploadRequiredFilter(c: Config): Either[File, S3MetaData] => Stream[IO, File] = { case Left(file) => { - println(s" Created: ${c.relativePath(file)}") + logger.info(s" Created: ${c.relativePath(file)}") Stream(file) } case Right(s3Metadata) => @@ -30,7 +31,7 @@ trait UploadSelectionFilter { filter { case (_, localHash) => localHash != s3Metadata.remoteHash }. map { case (localFile,_) => { - println(s" Updated: ${c.relativePath(localFile)}") + logger.info(s" Updated: ${c.relativePath(localFile)}") localFile } }