use of java.util.logging.FileHandler in project buck by facebook.
the class JavaUtilsLoggingBuildListener method ensureLogFileIsWritten.
public static void ensureLogFileIsWritten(ProjectFilesystem filesystem) {
if (!filesystem.exists(filesystem.getBuckPaths().getScratchDir())) {
try {
filesystem.mkdirs(filesystem.getBuckPaths().getScratchDir());
} catch (IOException e) {
throw new HumanReadableException(e, "Unable to create output directory: %s: %s: %s", filesystem.getBuckPaths().getScratchDir(), e.getClass(), e.getMessage());
}
}
try {
FileHandler handler = new FileHandler(filesystem.resolve(filesystem.getBuckPaths().getScratchDir()).resolve("build.log").toString(), /* append */
false);
Formatter formatter = new BuildEventFormatter();
handler.setFormatter(formatter);
LOG.setUseParentHandlers(false);
LOG.addHandler(handler);
LOG.setLevel(LEVEL);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of java.util.logging.FileHandler in project buck by facebook.
the class JavaUtilsLoggingBuildListener method closeLogFile.
public static void closeLogFile() {
for (Handler handler : LOG.getHandlers()) {
if (handler instanceof FileHandler) {
LOG.removeHandler(handler);
handler.close();
}
}
}
use of java.util.logging.FileHandler in project cogtool by cogtool.
the class CogTool method enableLogging.
public static void enableLogging(boolean value) {
if (value) {
logger.setLevel(Level.ALL);
for (Handler h : logger.getHandlers()) {
if (h instanceof FileHandler) {
return;
}
}
FileHandler fh = null;
try {
fh = new FileHandler((CogToolPref.LOG_DIRECTORY.getString() + LOG_FILE_PATTERN), LOG_FILE_MAX_SIZE, LOG_FILES_MAX_COUNT, true);
} catch (IOException ex) {
logger.warning("Couldn't create log file: " + ex);
return;
}
fh.setFormatter(new Formatter() {
@Override
public String format(LogRecord r) {
long ms = r.getMillis();
return String.format("%tF %tT.%tL\t%s\n", ms, ms, ms, r.getMessage());
}
});
logger.addHandler(fh);
} else {
logger.setLevel(DEFAULT_LOG_LEVEL);
for (Handler h : logger.getHandlers()) {
if (!(h instanceof ConsoleHandler)) {
logger.removeHandler(h);
}
}
}
}
use of java.util.logging.FileHandler in project malmo by Microsoft.
the class TCPSocketHelper method setLogging.
static void setLogging(boolean log) {
logging = log;
if (log == true && filehandler == null) {
try {
filehandler = new FileHandler("TCPLog.txt");
filehandler.setFormatter(new SimpleFormatter());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.addHandler(filehandler);
}
}
use of java.util.logging.FileHandler in project heron by twitter.
the class LoggingHelper method getFileHandler.
/**
* Initialize a <tt>FileHandler</tt> to write to a set of files
* with optional append. When (approximately) the given limit has
* been written to one file, another file will be opened. The
* output will cycle through a set of count files.
* The pattern of file name should be: ${processId}.log.index
* <p>
* The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
* properties (or their default values) except that the given pattern
* argument is used as the filename pattern, the file limit is
* set to the limit argument, and the file count is set to the
* given count argument, and the append mode is set to the given
* <tt>append</tt> argument.
* <p>
* The count must be at least 1.
*
* @param limit the maximum number of bytes to write to any one file
* @param count the number of files to use
* @param append specifies append mode
* @throws IOException if there are IO problems opening the files.
* @throws SecurityException if a security manager exists and if
* the caller does not have <tt>LoggingPermission("control")</tt>.
* @throws IllegalArgumentException if {@code limit < 0}, or {@code count < 1}.
* @throws IllegalArgumentException if pattern is an empty string
*/
public static FileHandler getFileHandler(String processId, String loggingDir, boolean append, int limit, int count) throws IOException, SecurityException {
String pattern = loggingDir + "/" + processId + ".log.%g";
FileHandler fileHandler = new FileHandler(pattern, limit, count, append);
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setEncoding("UTF-8");
return fileHandler;
}
Aggregations