Updated April 20, 2023
Introduction to Scala Logging
Logging is used to maintain the logs of an application. It is very much required to monitor our application. Logging helps us to monitor the activity of our application in case if any error occurs we can check it from the logs and fix it. We can also get the information about what activity is going on into out application. we can have day to day task and full monitoring of applications. It also keeps track of the unusual errors or circumstances which can lead down fall of our application. Majorly it is very helpful for the developers to keep track of all the actions performed and the error occurs if any.
Syntax:
In scala also we have three levels of logging which are debug, info and error. But we need to set this in our application configuration which level we want to use. This levels will be different according to different environment lets discuss them with syntax see below;
Debug level syntax; debug(our message)
Info level syntax; info(our message)
Error level syntax; error(our message)
In this way we can define them our application and we have to enable the log level accordingly and also make entries into configuration to make them work.
How Logging Work in Scala?
Scala logging is important and works like any other programming language. To keep track of our application we can use logging it can be used with different levels. To apply logging we can simply use scala logging for this. Scala provides us one logger class which is present inside this com.typesafe.scalalogging package also this class internally wraps the SLF4J logger. To use scala logging we have to pass or give a name to one of the method available in Logger object. We will see this by syntax see below;
We can pass the name in many different ways we can discuss them in details with one practice example for beginners for better understanding of logging concept;
1. In this we can pass the name directly to the Logger class.
vallogger_name = Logger("any_name")
In this syntax, we just have to give the name for the logger and one logging name pass as parameter inside the Logger class.
2. In this case, we can wrap our class by the implicit tag parameter.
vallogger_name = Logger[Your_Class_Name]
In this syntax, we are passing our class to the implicit tag parameter.
3. In this case, as we know this scala logging internally use SLF4J, so we can pass the name to the SLF4J logger instance also.
vallogger_name = Logger(LoggerFactory.getLogger("any_name"))
Give logger name and pass one parameter inside SLF4J instance also.
4. In this, we pass inside the class.
vallogger_name = Logger(classOf[Your_Class_Name])
Inside this package com.typesafe.scalalogging it provides us two traits which are Strict Logging and Lazy Logging and they are responsible to define the logger as Strict or Lazy.
Now we have one more this which is called Application logging: Application logging keeps track of our application at runtime and maintains all the logs. But sometime it may happen that we want all the logs of our application so we have to maintain the audit of logging and this can be achieved by storing our runtime application logs somewhere like database, into a disk, or search engine etc.
There are some things which all application wat to capture as a log;
- As we talked about the log levels i.e. information, error, and warning.
- The most important thing is the timestamp.
- Some relevant messages make any sense after some time also.
The log levels define various things see below;
- Info: This level gives us the normal information about the application that our application is working normal without issues. We can add different things here like we have a new entry into the database;our service has started or stopped this type of logs we can put here.
- Error: Error is something which is serious and shows something wring with our application it is not behaving normal as expected. This can be anything like related to database connection; some services are not available or stopped due to some failure etc. So this is very important to fix and make our application again up and running.
- Debug: Debug level is something like when we perform our diagnosis regarding the application. Like if we are making any request for data to the database so is it working properly and fetching all the data that is applicable regarding the request we have made.
- Trace: This is a more detailed version of the debug level. When we want to know the very precious value than we go for Trace in scala logging.
Examples of Scala Logging
In this example, we are using Print Writer and File to maintain our logs in scala. We will create a file into our system and going to maintain the logs there.
Example #1
Code:
import java.io.{File, PrintWriter}
object LoggingDemo extends App {
// here we are creting new file for logging!!!
val pw = new PrintWriter(new File("logging_demo.log"))
// here we are writing some logs to the file to maintian and monitor.
pw.write("logging file has been created")
pw.write("loggers working fine")
pw.close()
}
Output:
Example #2
In this example, we are using slf4j to log the activity of our program. We have using different methods available for logging like debug, error, and info.
Code:
import org.slf4j.LoggerFactory
object Main extends App {
// creating logger object using Logger factory
valmy_logger = LoggerFactory.getLogger(getClass.getSimpleName)
// calling info method
my_logger.info("Here we are using info method !!!! ")
// calling debug method
my_logger.debug("Here we are using debug method !!!! ")
// calling error method
my_logger.error("Here we are using error method !!!! ")
println("logges been created. !!!")
}
Output:
Conclusion
We use logging for monitoring purposes. Which helps us to behave our application as expected. We can also provide different logging levels as well. We can maintain any number of lines to log out application activity we can also store them somewhere into the file for the future purpose also these file representation makes it easier to understand.
Recommended Articles
We hope that this EDUCBA information on “Scala Logging” was beneficial to you. You can view EDUCBA’s recommended articles for more information.