-2

How to create log file in an existing directory ?..I have tried out this code:

String fileName = "/log/ABC.log";
FileHandler fileHandler = new FileHandler(fileName, true);

But it is not working.Here log is the directory...

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
user746458
  • 369
  • 2
  • 13
  • 26
  • 1
    Is any exception thrown? You need to tell us more than "it is not working". – FThompson Jul 15 '12 at 19:05
  • 1
    you need to go back and click on the checkmark and accept some of those answers to continue to get help –  Jul 15 '12 at 19:07
  • The exception is: Couldn't get lock for /log/ABC.log. If I create file directly ie String fileName = "ABC.log", the file is created.. – user746458 Jul 16 '12 at 05:59
  • I'm having the same issue that filehandler is not creating the file. How did you resolve this? – snikt Aug 13 '18 at 22:44

2 Answers2

1

You should take a look at the Logger class which is part of Java (since version 1.4). The FileHandler class is part of the logging package and it relies on the Logger class to actually log the errors to the FileHandler object you create.

Bobulous
  • 12,967
  • 4
  • 37
  • 68
0

In order to create a new log file in the current directory, do not prefix the file name with "/", such as in the following code:

FileHandler fileHandler = new FileHandler("logs/abc.log");
John Mikic
  • 640
  • 6
  • 11