本文整理匯總了Java中java.util.logging.FileHandler類的典型用法代碼示例。如果您正苦於以下問題:Java FileHandler類的具體用法?Java FileHandler怎麽用?Java FileHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
FileHandler類屬於java.util.logging包,在下文中一共展示了FileHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: Server
import java.util.logging.FileHandler; //導入依賴的package包/類
public Server(String fileName, boolean restart) {
super();
parseServerFile(fileName);
seatAssignments = new ArrayList<>(nSeats);
for (int i = 0; i < nSeats; i++)
seatAssignments.add("");
log.getParent().removeHandler(log.getParent().getHandlers()[0]);
try {
// check if logs dir exists
File logDir = new File("./logs/");
if( !(logDir.exists()) )
logDir.mkdir();
FileHandler fh = new FileHandler("logs/server_log_" + serverID + ".log");
fh.setFormatter(new SimpleFormatter());
fh.setLevel(logLevel);
log.addHandler(fh);
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(logLevel);
log.addHandler(ch);
log.setLevel(logLevel);
log.info("Server initializing...");
log.info("ServerID = " + serverID);
log.info("nServers = " + nServers);
log.info("nSeats = " + nSeats);
log.info("my tcp port = " + tcpPort);
for (int i = 0; i < nServers; i++)
log.info("Server " + i + ": " + servers.get(i) + ":" + ports.get(i));
log.info("Server init complete");
log.info("--------------------------------");
mutex = new LamportMutex(servers, ports, this, restart);
mutex.init();
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
}
示例2: main
import java.util.logging.FileHandler; //導入依賴的package包/類
public static void main(String[] args)
{
try
{
LogManager lm = LogManager.getLogManager();
Logger logger = Logger.getLogger("BasicLogging");
FileHandler fh = new FileHandler("log_test.txt");
lm.addLogger(logger);
logger.addHandler(fh);
logger.log(Level.INFO, "test 1");
logger.log(Level.INFO, "test 2");
fh.close();
}
catch (Exception e) {
System.err.println("Exception thrown: " + e);
e.printStackTrace();
}
}
示例3: testFileHandlerClose
import java.util.logging.FileHandler; //導入依賴的package包/類
private static void testFileHandlerClose(File writableDir) throws IOException {
File fakeLock = new File(writableDir, "log.log.lck");
if (!createFile(fakeLock, false)) {
throw new IOException("Can't create fake lock file: " + fakeLock);
}
try {
List<File> before = listLocks(writableDir, true);
System.out.println("before: " + before.size() + " locks found");
FileHandler handler = createFileHandler(writableDir);
System.out.println("handler created: " + handler);
List<File> after = listLocks(writableDir, true);
System.out.println("after creating handler: " + after.size() + " locks found");
handler.close();
System.out.println("handler closed: " + handler);
List<File> afterClose = listLocks(writableDir, true);
System.out.println("after closing handler: " + afterClose.size() + " locks found");
afterClose.removeAll(before);
if (!afterClose.isEmpty()) {
throw new RuntimeException("Zombie lock file detected: " + afterClose);
}
} finally {
if (fakeLock.canRead()) delete(fakeLock);
}
List<File> finalLocks = listLocks(writableDir, false);
System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
示例4: AptaLogger
import java.util.logging.FileHandler; //導入依賴的package包/類
private AptaLogger() throws IOException{
//instance the file handler
filepath = Paths.get(Configuration.getParameters().getString("Experiment.projectPath"), "log_" + formatter.format(new Date()) + ".txt").toString();
fileHandler = new FileHandler(filepath,true);
fileHandler.setLevel(Level.ALL); // We want everything logged on file
//instance the logger
logger = Logger.getLogger(AptaLogger.class.getName());
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
//instance formatter, set formatting, and handler
formatterTxt = new AptaLoggerFormat();
fileHandler.setFormatter(formatterTxt);
logger.addHandler(fileHandler);
}
示例5: LogService
import java.util.logging.FileHandler; //導入依賴的package包/類
private LogService(Logger logger) {
super(logger);
// setup a log file if the execution mode can access the filesystem (e.g. not for RA)
// we want our logfile to look the same regardless of any user settings, so ignore
// a possible user logging property config file
if (RapidMiner.getExecutionMode().canAccessFilesystem()) {
try {
FileHandler logFileHandler = new FileHandler(FileSystemService.getLogFile().getAbsolutePath(), false);
logFileHandler.setLevel(Level.ALL);
logFileHandler.setFormatter(new SimpleFormatter());
LogService.getRoot().addHandler(logFileHandler);
} catch (IOException e) {
LogService.getRoot().log(Level.WARNING, "com.rapidminer.logservice.logfile.failed_to_init", e.getMessage());
}
}
}
示例6: initGlobals
import java.util.logging.FileHandler; //導入依賴的package包/類
/**
* Initializes all globals to a default state. Used so that in the case of a
* memory leak, the previous values are not accidentally used.
*/
public static void initGlobals() {
System.setProperty("java.util.logging.SimpleFormatter.format",
"(%1$tc) [%4$s] ~%2$s~%nMessage: \"%5$s\"%n");
try {
handler = new FileHandler("log.txt");
handler.setLevel(Level.INFO);
handler.setFormatter(new SimpleFormatter());
// Ensure we only log to log file.
LOGGER.setUseParentHandlers(false);
LOGGER.addHandler(handler);
LOGGER.setLevel(Level.INFO);
} catch (IOException | SecurityException ex) {
throw new RuntimeException("Failed to initialized logger... " + ex.getMessage());
}
PROJECT_FOLDER = null;
URI_PREFIX = null;
CURRENT_FILE = null;
}
示例7: configureLogger
import java.util.logging.FileHandler; //導入依賴的package包/類
protected void configureLogger() throws SecurityException, IOException {
if (logger == null) {
logger = Logger.getLogger(LOGGER_NAME);
}
logger.setUseParentHandlers(false);
logFileHandler = new FileHandler(logFilePath);
logFileHandler.setFormatter(new SimpleFormatter());
Level logLevel = Level.INFO;
try {
logLevel = Level.parse(logLevelString);
} catch (IllegalArgumentException e) {
logInfo("Unrecognized log level :" + logLevelString + " defaulting to :" + logLevel);
}
logFileHandler.setLevel(logLevel);
logger.addHandler(logFileHandler);
}
示例8: main
import java.util.logging.FileHandler; //導入依賴的package包/類
public static void main(String[] args) throws SecurityException, IOException {
Logger logger = Logger.getGlobal();
logger.setLevel(Level.FINER);
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT %4$s %2$s %5$s%6$s%n");
SimpleFormatter formatterTxt = new SimpleFormatter();
FileHandler logFile = new FileHandler("log2.txt");
logFile.setLevel(Level.FINER);
logFile.setFormatter(formatterTxt);
logger.addHandler(logFile);
// Create and set handler
Handler systemOut = new StdoutConsoleHandler();
systemOut.setLevel(Level.ALL);
logger.addHandler(systemOut);
logger.setLevel(Level.SEVERE);
// Prevent logs from processed by default Console handler.
logger.setUseParentHandlers(false); // Solution 1
new MiniBrassCompiler().doMain(args);
}
示例9: init
import java.util.logging.FileHandler; //導入依賴的package包/類
/**
* Creates a new logger
*/
private Logger init(final Level level, final String file) {
final LogSettings logSettings = settingsService.getLogSettings();
final Logger logger = Logger.getAnonymousLogger();
logger.setLevel(level);
logger.setUseParentHandlers(false);
try {
final FileUnits units = logSettings.getMaxLengthPerFileUnits();
final FileHandler fileHandler = new FileHandler(file, units.calculate(logSettings.getMaxLengthPerFile()), logSettings.getMaxFilesPerLog(), true);
fileHandler.setFormatter(logFormatter);
fileHandler.setEncoding(settingsService.getLocalSettings().getCharset());
logger.addHandler(fileHandler);
} catch (final Exception e) {
final ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(logFormatter);
try {
consoleHandler.setEncoding(settingsService.getLocalSettings().getCharset());
} catch (final Exception e1) {
// Just ignore
}
logger.addHandler(consoleHandler);
logger.log(Level.WARNING, "Unable to create logger for file " + file);
}
return logger;
}
示例10: main
import java.util.logging.FileHandler; //導入依賴的package包/類
/**
* Main entry point for the OpenOffice.org Bean Test.
*
* @param args arguments of the test
*
* @author Andreas Bröker
* @date 21.05.2006
*/
public static void main(String[] args) throws OfficeApplicationException {
LogManager.getLogManager().reset();
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINEST);
LOGGER.addHandler(consoleHandler);
LOGGER.setLevel(Level.FINEST);
try {
FileHandler fileHandler = new FileHandler("log.xml");
fileHandler.setLevel(Level.FINEST);
LOGGER.addHandler(fileHandler);
} catch (Throwable throwable) {
}
OfficeBeanTest testOfficeBean = new OfficeBeanTest();
if (args.length == 0) {
testOfficeBean.test(null);
}else if (args.length == 1) {
testOfficeBean.test(args[0]);
}else if(args.length==4){
testOfficeBean.remoteTestPDF(new File(args[2]), new File(args[3]), args[0], Integer.valueOf(args[1]));
}else{
System.out.println("usage:\nOfficeBeanTest host port source-odt target-pdf\nOfficeBeanTest officeHome");
}
}
示例11: addNewSeleniumFileHandler
import java.util.logging.FileHandler; //導入依賴的package包/類
private static File addNewSeleniumFileHandler(Logger currentLogger,
LoggingOptions options) {
try {
FileHandler fileHandler;
final File logFile;
logFile = options.getLogOutFile();
fileHandler = seleniumFileHandlers.get(logFile);
if (fileHandler == null) {
fileHandler = registerNewSeleniumFileHandler(logFile);
}
fileHandler.setFormatter(new TerseFormatter(true));
currentLogger.setLevel(Level.FINE);
fileHandler.setLevel(Level.FINE);
currentLogger.addHandler(fileHandler);
return logFile;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例12: setupLogging
import java.util.logging.FileHandler; //導入依賴的package包/類
private void setupLogging() {
log.setUseParentHandlers(false);
LogFormatter formatter = new LogFormatter();
ConsoleHandler ch = new ConsoleHandler();
ch.setFormatter(formatter);
if (isDebugging()) {
log.setLevel(Level.FINEST);
ch.setLevel(Level.FINEST);
} else {
log.setLevel(Level.INFO);
ch.setLevel(Level.INFO);
}
log.addHandler(ch);
try {
FileHandler fh = new FileHandler("server.log");
fh.setFormatter(formatter);
if (isDebugging()) {
fh.setLevel(Level.FINEST);
} else {
ch.setLevel(Level.INFO);
}
log.addHandler(fh);
} catch (IOException ex) {
log.log(Level.SEVERE, "Error while adding FileHandler to logger. Logs will not be output to a file.", ex);
}
}
示例13: setupLogging
import java.util.logging.FileHandler; //導入依賴的package包/類
public static void setupLogging(boolean logToConsole, String logFile) throws Exception {
// Setup the logging
System.setProperty(
"java.util.logging.SimpleFormatter.format",
"%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS: %4$s: %5$s%n%6$s%n");
LogManager.getLogManager().reset();
Logger rootLogger = Logger.getLogger("");
if(logFile != null) {
FileHandler logHandler = new FileHandler(logFile, 8*1024*1024, 2, true);
logHandler.setFormatter(new SimpleFormatter());
logHandler.setLevel(Level.FINEST);
rootLogger.addHandler(logHandler);
}
if(logToConsole) {
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(new SimpleFormatter());
consoleHandler.setLevel(Level.INFO);
rootLogger.addHandler(consoleHandler);
}
rootLogger.setLevel(Level.INFO);
}
示例14: initLog
import java.util.logging.FileHandler; //導入依賴的package包/類
private static void initLog()
{
Formatter simpleFormatter;
try
{
String path = new File(".").getCanonicalPath();
String logName = String.valueOf(System.currentTimeMillis());
simpleFormatter = new SimpleFormatter();
fileHandler = new FileHandler(path + logName + ".log");
fileHandler.setFormatter(simpleFormatter);
fileHandler.setLevel(Level.ALL);
}
catch (IOException e)
{
errorLog.log(Level.WARNING, e.getMessage(), e);
}
}
示例15: main
import java.util.logging.FileHandler; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
/* Ensure directory has been created */
new File("logs").mkdir();
/* Get the date to be used in the filename */
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
Date now = new Date();
String date = df.format(now);
/* Set up the filename in the logs directory */
String logFileName = "logs/testlog-" + date + ".txt";
/* Set up logger */
FileHandler myFileHandler = new FileHandler(logFileName);
myFileHandler.setFormatter(new SimpleFormatter());
Logger ocajLogger = Logger.getLogger("OCAJ Logger");
ocajLogger.setLevel(Level.ALL);
ocajLogger.addHandler(myFileHandler);
/* Log Message */
ocajLogger.info("\nThis is a logged information message.");
/* Close the file */
myFileHandler.close();
}