How can I append to log files in this simple Java Logging implementation?
Solution 1
From the FileHandler
constructor, you can specify a boolean
to specify an append mode.
Do as following:
fh = new FileHandler("%h/TorrentDownloader.log", true);
Solution 2
Use a different constructor
fh = new FileHandler("%h/TorrentDownloader.log", true);
Solution 3
You can use this constructor:
FileHandler handler = new FileHandler(String pattern, boolean append);
and in your case, it's:
fh = new FileHandler("%h/TorrentDownloader.log", true);
This constructor creates a FileHandler with a file name pattern, and a boolean telling whether the FileHandler should append to any existing files or not.
And this article has a full explanation.
diegoaguilar
I'm a mexican backend web developer interested in performance and infrastructure development. I've been working with Node/express mainly for last 3 years doing API's design and implementation. Been pretty involved with MEAN apps too, and pretty involved with MongoDB in particular. I really can say I love express before frameworks. About languages, Right now I'm pretty involved with Javascript, with new ES6 and ES7 stuff. I've got experience with Java and Python too, can even work a bit in Android what I've liked. Would love to get to know Go, Elixir and these trending functional programming languages soon ;) Image processing and natural language processing are challenging topics for me, and I'd love to lead my career to high performance and availability aplications and stacks development. I rather do backend development but lately I've been trying React, which I enjoy. Apart from software development, I love Real Madrid and watching movies and series, of course music, and I also love food, like a lot.
Updated on June 26, 2022Comments
-
diegoaguilar almost 2 years
I got the following class to create and manage a
Logger
. Whenever across the code and program execution, calls to staticgetLogger()
catch blocks are used to log.public class Log { private static final Logger logger = Logger.getLogger("MyLog"); public static void iniciarLog() throws IOException { FileHandler fh; try { // fh = new FileHandler(System.getProperty("user.home")+System.getProperty("file.separator")+"TorrentDownloader.log"); fh = new FileHandler("%h/TorrentDownloader.log"); logger.addHandler(fh); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); logger.info("Se inició el log"); } catch (SecurityException | IOException e) { logger.severe("Error al crear el log"); } } public static Logger getLogger() { return logger; } }
However, how can I append to such logging file? All examples I've seen change a lot this implementation which I like as it's clear, brief and simple.
-
diegoaguilar almost 10 yearsThanks, can you extend more about the
count
parameter? -
calimbak almost 10 yearsOops ... my bad, i misread the documentation, it's a default behaviour. I will update my answer.
-
diegoaguilar almost 10 yearsBut, how would it change to use that constructor giving a different count?
-
calimbak almost 10 yearsIt limit the number of files created with the limit parameter. Check the link in my answer for more information.
-
diegoaguilar almost 10 yearsThanks. By the way, do you have experience with FX and data binding? I got this doubt: stackoverflow.com/questions/23730904/…