Book Image

Lift Application Development Cookbook

By : Gilberto Tadeu Garcia Jun
Book Image

Lift Application Development Cookbook

By: Gilberto Tadeu Garcia Jun

Overview of this book

Developing secure web applications is one of the most important tasks developers have to deal with. With Lift, it is easy to create solid and formidable web applications as it is the most secure web framework available today. The View-First approach and being able to handle things as purely data transformation, makes working with Lift an exciting task. "Lift Application Development Cookbook" teaches you how to build web applications using this amazing framework. The book moves gradually, starting with the basics (starting a new project, submitting a form, and so on) before covering more advanced topics such as building a REST API and integrating your application with other technologies and applications. "Lift Application Development Cookbook" takes you on a journey of creating secure web applications. Step-by-step instructions help you understand how things work and how various elements relate to each other. You'll learn different ways to process a form, build dynamic HTML pages, and create an API using REST. You'll also learn how to work with relational and NoSQL databases and how to integrate your application with other technologies as well as with third-part applications such as Gmail and Facebook. By the end of the book, you will be able to understand how Lift works and be able to build web applications using this amazing and exciting framework.
Table of Contents (15 chapters)
Lift Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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