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

Commit

Permalink
#12: export loglevel setters to JS
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Apr 4, 2017
1 parent e502133 commit bc4f0ce
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ wvlet-log is a libray for enhancing your Scala application logging with colors a
```scala
libraryDependencies += "org.wvlet" %% "wvlet-log" % "(version)"

// For Scala.js (Since 1.2)
// For Scala.js (Since wvlet-log 1.2)
libraryDependencies += "org.wvlet" %%% "wvlet-log" % "(version)"
```

Expand Down Expand Up @@ -171,6 +171,29 @@ Logger.setDefaultFormatter(CustomLogFormatter)
```
See also the examples in [LogFormat.scala](src/main/scala/wvlet/log/LogFormat.scala):

### Using wvlet-log in Scala.js


```scala
import wvlet.log._

Logger.setDefaultHandler(JSConsoleLogHandler())

class YourAppClass extends LogSupport {

info("hello")
}
```

The log message will be showin in your browser's developer console.

To configure the log level, use `wvlet.log.setDefaultLogLevel` or `wvlet.log.setLogLevel`:
```javascript
> wvlet.log.setDefaultLogLevel("debug")

> wvlet.log.setLogLevel("your.logger.name", "trace")
```

### Using with slf4j

If you are using slf4j, just add `slf4j-jdk14` to your dependency. The log messages from slf4j will be sent to wvlet-log:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ class JSConsoleLogHandler(logColorPalette: JSLogColorPalette = JSConsoleLogHandl
}

object JSConsoleLogHandler {

val DEFAULT_COLOR_PALETTE : JSLogColorPalette = new JSLogColorPalette()

def apply() : JSConsoleLogHandler = new JSConsoleLogHandler(DEFAULT_COLOR_PALETTE)
def apply(colorPalette:JSLogColorPalette) = new JSConsoleLogHandler(colorPalette)

case class JSLogColorPalette(
error:String = "#D32F2F",
warn:String = "#E64A19",
Expand Down
21 changes: 21 additions & 0 deletions wvlet-log/js/src/main/scala/wvlet/log/JSLogger.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wvlet.log

import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}

/**
*
*/
@JSExportTopLevel("wvlet.log")
object JSLogger {
@JSExport
def setDefaultLogLevel(level:String) : Boolean = {
Logger.setDefaultLogLevel(LogLevel(level))
true
}

@JSExport
def setLogLevel(loggerName:String, level:String) : Boolean = {
Logger(loggerName).setLogLevel(LogLevel(level))
true
}
}
3 changes: 2 additions & 1 deletion wvlet-log/js/src/main/scala/wvlet/log/LogEnv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.io.PrintStream
/**
*
*/

private[log] object LogEnv extends LogEnvBase {
override def isScalaJS: Boolean = true
override def defaultLogLevel: LogLevel = LogLevel.INFO
Expand All @@ -16,7 +17,7 @@ private[log] object LogEnv extends LogEnvBase {

// In Scala.js we cannot use cl.getInterfaces to find the actual type
val pos = name.indexOf("$")
if(pos > 0) {
if (pos > 0) {
// Remove trailing $xxx
name = name.substring(0, pos)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class JSConsoleLogHandlerTest extends Spec {

"JSConsoleLogHandler" should {

Logger.setDefaultHandler(new JSConsoleLogHandler)
Logger.setDefaultHandler(JSConsoleLogHandler())

error("error message")
warn("warn message")
Expand Down

0 comments on commit bc4f0ce

Please sign in to comment.