官术网_书友最值得收藏!

Logging using logback

Logback is a log engine that is intended to be a better version of the popular log4j. You might want to check the project website to understand the reasons you should consider Logback over log4j. But, you might ask why we need a log engine at all?

Logging is a useful tool that allows us to track what happens with the applications we build that are running on environments which we, as developers, cannot easily debug. That's why it is important that the tools we use to build applications support logging in an easy way. Lift does support logging in an easy and flexible manner. So, let us see how to log and what happens with our application.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Getting ready

We will use the code from the last section to re-use the SiteMap we have defined.

How to do it...

Don't worry if you don't fully understand the following code. We will get back to it later in the book.

  1. Create a new Scala class called ListUser inside the snippet package with the following code:
    class ListUser extends Logger {
      def log(text: String) {
        text match {
          case str if str.length == 0 =>
            error("user with no name")
          case str if str == "Forbidden" =>
            warn("this user shouldn't have access")
          case str =>
            debug("User name: " + str)
        }
      }
      def list = {
        val users = List("John", "Sarah", "Peter", "Sam", "","Forbidden")
        info("listing users")
        "li .name *" #> users.map {
          user => {
            log(user)
            Text(user)
          }
        }
      }
    }
  2. Add the following HTML snippet in /webapp/contacts/list.html:
    <ul data-lift="ListUser.list">
      <li class="name"><!-- user names will be in here --></li>
    </ul>
  3. Change the code.snippet logger in src/main/resources/logback.xml to debug the level as follows:
    <logger name="code.snippet" level="debug" />
  4. Start the application and go to http://localhost:8080/contacts/list in your browser.

    In your browser, you will see, a page similar to the one shown in the following screenshot:

    In the SBT console, you will see a log similar to the following screenshot:

How it works...

We configured the log in the logback.xml file. You might notice that there was an appender already configured. Appenders are the components that actually do the task of writing log events. There are several types of appenders and one of them is the ConsoleAppender that just prints the log entries in the standard output—the console in this case.

The class ListUser is a Lift snippet that traverses the list of strings defined by variable users. For each string, it does two things:

  • Invokes the log method
  • Adds the string inside the li tag

Then it returns the modified HTML to be rendered in the browser.

The log method will create a log entry with different log levels depending on the string's content, by invoking the method from the Logger trait which includes info, debug, error, warning, and so on.

The Logger trait's methods are accessible from the snippet because we mixed them in our snippet by extending them using extends Logger. Then the snippet is invoked by the instruction ListUser.list that we added in the data-lift attribute in the HTML file.

There's more...

When you use the Logger trait, Lift will create one logger per class or object. That is why we can see the object or class where the log entry was created. You can see in our example that the log was created by the ListUser snippet by extending the trait.

This is a nice feature since we can have more control of how our application creates the log entries. We can, for example, set packages with different log levels. As you can see in the logback.xml file, the code.snippet has the debug level while net.liftweb has the warn level and bootstrap.liftweb has the info level.

Logback is not the only log engine available. If you want to use log4j, you will need to change the dependency in the build.sbt file from:

"ch.qos.logback"    % "logback-classic"     % "1.0.6",

To:

"org.slf4j" % "slf4j-log4j12" % "1.6.1",

Also, you will need to create src/main/resources/log4j.xml, which is log4j's configuration file.

See also

主站蜘蛛池模板: 深州市| 萝北县| 夏邑县| 哈巴河县| 桐城市| 凤庆县| 奎屯市| 基隆市| 佛坪县| 镇原县| 山丹县| 曲水县| 惠东县| 盐边县| 长葛市| 桦南县| 扎赉特旗| 琼中| 昌图县| 汪清县| 高阳县| 九龙县| 沙洋县| 独山县| 麻城市| 盐山县| 四川省| 深州市| 陆川县| 武邑县| 涡阳县| 凌云县| 屯留县| 沾化县| 博乐市| 金山区| 广水市| 丘北县| 漯河市| 张家界市| 伊吾县|