- Lift Application Development Cookbook
- Gilberto T. Garcia Jr.
- 755字
- 2021-08-04 10:05:42
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.
- 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) } } } }
- 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>
- Change the
code.snippet
logger insrc/main/resources/logback.xml
to debug the level as follows:<logger name="code.snippet" level="debug" />
- 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
- You can find more about log4j at the following URL:
- Check this URL if you want to learn more about Logback:
- At last, you can find more information about other ways to use Lift's log engine, such as how to create a singleton logger object for the entire application, as shown here:
- Designing Machine Learning Systems with Python
- C程序設計簡明教程(第二版)
- Android項目開發入門教程
- INSTANT OpenCV Starter
- PHP程序設計(慕課版)
- Git高手之路
- Wireshark Network Security
- Visual Basic程序設計教程
- 前端架構:從入門到微前端
- Mastering Rust
- The Complete Coding Interview Guide in Java
- Apache Kafka Quick Start Guide
- Advanced Express Web Application Development
- Android項目實戰:手機安全衛士開發案例解析
- Java程序員面試筆試寶典(第2版)