-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoRenameLogger.java
79 lines (71 loc) · 2.63 KB
/
PhotoRenameLogger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package photo_renamer;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.logging.*;
/**
* A Singleton object that logs ImageFile rename events.
* Only one instance can exist.
*/
public class PhotoRenameLogger {
/** The Logger used by the PhotoRenameLogger */
private static final Logger logger = Logger.getLogger(PhotoRenameLogger.class.getName());
/** The pathname of the log the PhotoRenameLogger works in. */
String logPath = System.getProperty("user.dir") + "/log.txt";
/**
* Construct a logger to rename images on the system.
*
* @throws SecurityException a SecurityException exception
* @throws IOException an IOException exception
*/
private PhotoRenameLogger() throws IOException {
File logFile = new File(logPath);
logFile.createNewFile();
Handler fileHandler = new FileHandler(logFile.getAbsolutePath(), true);
logger.setLevel(Level.ALL);
fileHandler.setLevel(Level.ALL);
fileHandler.setFormatter(new SimpleFormatter());
logger.addHandler(fileHandler);
}
/**
* Return the single instance of PhotoRenameLogger.
*
* @return PhotoRenameLogger
*/
static PhotoRenameLogger getInstance() {
return PhotoRenameLoggerHolder.INSTANCE;
}
/** A nested class to contain the single instance of PhotoRenameLogger. */
private static class PhotoRenameLoggerHolder {
/** The PhotoRenameLogger instance. */
private static final PhotoRenameLogger INSTANCE;
// Attempt to assign a PhotoRenameLogger instance to INSTANCE.
static {
PhotoRenameLogger loggerInstance;
try {
loggerInstance = new PhotoRenameLogger();
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "Failed to create logger instance!");
e.printStackTrace();
loggerInstance = null;
}
INSTANCE = loggerInstance;
}
}
/**
* Log rename events in the PhotoRenameLogger logger.
*
* @param o the renamed ImageFile to be logged.
* @param oldName the old name of the ImageFile.
*/
void logRename(Object oldName, Observable o) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String formattedDate = dateFormat.format(date);
logger.log(Level.FINE, "(" + oldName + " >>> " + o + " @ " + formattedDate + ")");
}
}