I am new to Scala and I am struggling to find out how I can redirect my logs to a file in Scala. This is a simple task in Python but I can't find the relevant documentation for Scala. I am trying to use log4j but I don't mind to use other packages either. All references that I find discuss how to do so through a configuration file but I would like to do this programmatically.
This is what I have found so far and works but I do not know how to add a file. I think FileAppender
should solve my problem but I can't find an example how to add it to my logger:
import org.apache.log4j.Logger
val logger = Logger.getLogger("My Logger")
logger.info("I am a log message")
What I wish to achieve (with some extra details) can be written in Python as follows:
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
handler = logging.FileHandler('output.log')
handler.setLevel(logging.INFO)
logger.addHandler(handler)
logger.info("I am a log message")