From e27a1a080041a0a8ce4cf90415a9ac61d3ab508f Mon Sep 17 00:00:00 2001 From: Rahul Phulore Date: Wed, 14 Mar 2012 03:04:18 +0530 Subject: [PATCH 1/4] Added a kestrel combinator for logging --- build.sbt | 1 + src/main/scala/LogUtilities.scala | 16 ++++++++++++++++ src/main/scala/Logging.scala | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/main/scala/LogUtilities.scala diff --git a/build.sbt b/build.sbt index 81677b3..1d24c93 100644 --- a/build.sbt +++ b/build.sbt @@ -13,6 +13,7 @@ crossScalaVersions := Seq( "2.9.0-1", "2.9.1") + libraryDependencies ++= Seq( "org.slf4j" % "slf4j-api" % "1.6.1") diff --git a/src/main/scala/LogUtilities.scala b/src/main/scala/LogUtilities.scala new file mode 100644 index 0000000..49a96b9 --- /dev/null +++ b/src/main/scala/LogUtilities.scala @@ -0,0 +1,16 @@ +package com.weiglewilczek.slf4s + +trait LogUtilities { + implicit def wrapAny[A](value: A) = new AnyWrapper(value) + + class AnyWrapper[A](value: A) { + // TODO: Add a comment explaining the usage. + def trace(message: => String, logFn: Logger => (=> String) => Unit = _.debug) + (implicit logger: Logger): A = { + logFn(logger)(message + ": " + value) + value + } + } +} + +object LogUtilities extends LogUtilities \ No newline at end of file diff --git a/src/main/scala/Logging.scala b/src/main/scala/Logging.scala index c3dfab1..d0d416b 100644 --- a/src/main/scala/Logging.scala +++ b/src/main/scala/Logging.scala @@ -23,5 +23,5 @@ trait Logging { /** * Logger for the type mixed into. */ - protected[slf4s] lazy val logger = Logger(this.getClass) + protected[slf4s] implicit lazy val logger = Logger(this.getClass) } From 394c6207556aaf17fd2e0d24a2d0ce7906f932cc Mon Sep 17 00:00:00 2001 From: missingfaktor Date: Wed, 14 Mar 2012 03:51:26 +0530 Subject: [PATCH 2/4] Added an explanatory comment --- src/main/scala/LogUtilities.scala | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/scala/LogUtilities.scala b/src/main/scala/LogUtilities.scala index 49a96b9..b360a30 100644 --- a/src/main/scala/LogUtilities.scala +++ b/src/main/scala/LogUtilities.scala @@ -4,7 +4,23 @@ trait LogUtilities { implicit def wrapAny[A](value: A) = new AnyWrapper(value) class AnyWrapper[A](value: A) { - // TODO: Add a comment explaining the usage. + + // This is a logging version of Kestrel combinator. + // This method is very similar to `tap` method in Ruby. It prints the object (along with the given message) + // on which the method was invoked, and evaluates to the same object. It is used to tap into method chains, + // in order to inspect the values of object at various stages in processing. + // + // Example: + // List.range(1, 10).trace("Original") + // .map(2 *).trace("After mapping") + // .filter(4 <).trace("After filtering") + // .size.trace("Size") + // + // The default logging method invoked is `debug`. It is easy to invoke other logging methods instead, if + // so required. + // + // Example: + // List.range(1, 10).trace("Original", _.info) def trace(message: => String, logFn: Logger => (=> String) => Unit = _.debug) (implicit logger: Logger): A = { logFn(logger)(message + ": " + value) From 6bc3bd664f683497f23e1876870cf8fdf79680a5 Mon Sep 17 00:00:00 2001 From: missingfaktor Date: Fri, 16 Mar 2012 00:40:56 +0530 Subject: [PATCH 3/4] Improved documentation. --- src/main/scala/LogUtilities.scala | 32 ------------------- src/main/scala/LoggingUtilities.scala | 45 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 32 deletions(-) delete mode 100644 src/main/scala/LogUtilities.scala create mode 100644 src/main/scala/LoggingUtilities.scala diff --git a/src/main/scala/LogUtilities.scala b/src/main/scala/LogUtilities.scala deleted file mode 100644 index b360a30..0000000 --- a/src/main/scala/LogUtilities.scala +++ /dev/null @@ -1,32 +0,0 @@ -package com.weiglewilczek.slf4s - -trait LogUtilities { - implicit def wrapAny[A](value: A) = new AnyWrapper(value) - - class AnyWrapper[A](value: A) { - - // This is a logging version of Kestrel combinator. - // This method is very similar to `tap` method in Ruby. It prints the object (along with the given message) - // on which the method was invoked, and evaluates to the same object. It is used to tap into method chains, - // in order to inspect the values of object at various stages in processing. - // - // Example: - // List.range(1, 10).trace("Original") - // .map(2 *).trace("After mapping") - // .filter(4 <).trace("After filtering") - // .size.trace("Size") - // - // The default logging method invoked is `debug`. It is easy to invoke other logging methods instead, if - // so required. - // - // Example: - // List.range(1, 10).trace("Original", _.info) - def trace(message: => String, logFn: Logger => (=> String) => Unit = _.debug) - (implicit logger: Logger): A = { - logFn(logger)(message + ": " + value) - value - } - } -} - -object LogUtilities extends LogUtilities \ No newline at end of file diff --git a/src/main/scala/LoggingUtilities.scala b/src/main/scala/LoggingUtilities.scala new file mode 100644 index 0000000..c20b91d --- /dev/null +++ b/src/main/scala/LoggingUtilities.scala @@ -0,0 +1,45 @@ +package com.weiglewilczek.slf4s + +/* + * A mixin with logging utilities. + */ +trait LoggingUtilities { + implicit def wrapAny[A](value: A) = new AnyWrapper(value) + + class AnyWrapper[A](value: A) { + + /** + * This is a logging version of Kestrel combinator. + * + * This method is very similar to Object#tap method in Ruby. It prints the given message along with the object + * on which the method was invoked, and evaluates to the same object. It is used to tap into method chains, in + * in order to inspect the values of object at various stages in processing. + * + * Example: + * List.range(1, 10).trace("Original") + * .map(2 *).trace("After mapping") + * .filter(4 <).trace("After filtering") + * .size.trace("Size") + * + * The default logging method invoked is LOgger#debug. It is easy to invoke other logging methods instead, if + * so required. + * + * Example: + * List.range(1, 10).trace("Original", _.info) + * + * @param message The message to be logged along with the object value. + * @param logFn The logging method to use. By default, set to Logger#debug. + * @param logger The logger to use for logging. + * @return The object on which the method was invoked. + */ + def trace(message: => String, logFn: Logger => (=> String) => Unit = _.debug)(implicit logger: Logger): A = { + logFn(logger)(message + ": " + value) + value + } + } +} + +/* + * A first class module that provides logging utilities. + */ +object LoggingUtilities extends LoggingUtilities \ No newline at end of file From 764e9807cd554a88808f2c3de4b108f9277fdc68 Mon Sep 17 00:00:00 2001 From: missingfaktor Date: Fri, 16 Mar 2012 11:33:26 +0530 Subject: [PATCH 4/4] Typo. --- src/main/scala/LoggingUtilities.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/LoggingUtilities.scala b/src/main/scala/LoggingUtilities.scala index c20b91d..87edfb9 100644 --- a/src/main/scala/LoggingUtilities.scala +++ b/src/main/scala/LoggingUtilities.scala @@ -21,7 +21,7 @@ trait LoggingUtilities { * .filter(4 <).trace("After filtering") * .size.trace("Size") * - * The default logging method invoked is LOgger#debug. It is easy to invoke other logging methods instead, if + * The default logging method invoked is Logger#debug. It is easy to invoke other logging methods instead, if * so required. * * Example: