本文整理汇总了Java中java.util.logging.FileHandler.setFormatter方法的典型用法代码示例。如果您正苦于以下问题:Java FileHandler.setFormatter方法的具体用法?Java FileHandler.setFormatter怎么用?Java FileHandler.setFormatter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.logging.FileHandler
的用法示例。
在下文中一共展示了FileHandler.setFormatter方法的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: 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);
}
示例3: 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());
}
}
}
示例4: 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;
}
示例5: 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);
}
示例6: 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;
}
示例7: 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);
}
}
示例8: 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);
}
示例9: 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();
}
示例10: 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());
}
}
}
示例11: logToFile
import java.util.logging.FileHandler; //导入方法依赖的package包/类
public static void logToFile(Logger logger, String name, Level level) {
try {
File aux = new File(".");
String parent = aux.getCanonicalPath();
File f = new File(parent + File.separator + "log");
if (!f.isDirectory()) {
boolean made = f.mkdir();
if (!made) {
logger.warning("Impossible to create log directory");
}
}
String path = parent + File.separator + "log" + File.separator
+ name + "%u.log";
FileHandler fh = new FileHandler(path);
fh.setFormatter(new SimpleFormatter());
fh.setEncoding("UTF-8");
logger.addHandler(fh);
logger.setLevel(level);
} catch (IOException e) {
logger.warning("Error configuring the log file.");
e.printStackTrace();
}
}
示例12: initLogs
import java.util.logging.FileHandler; //导入方法依赖的package包/类
/**
* 初始化日志记录
*/
private void initLogs() {
try {
File file = new File(System.getProperty("user.dir") + "/logs");
if (!file.exists()) {
file.mkdirs();
}
FileHandler fileHandler = new FileHandler(file.getPath() + "/log.log");
fileHandler.setLevel(Level.ALL);
fileHandler.setFormatter(new SimpleFormatter());
LOGGER.addHandler(fileHandler);
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
示例13: init
import java.util.logging.FileHandler; //导入方法依赖的package包/类
@PostConstruct
public void init() {
try {
File log = new File(logFile);
File parentDir = log.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
FileHandler fh = new FileHandler(logFile);
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
} catch (Exception e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
示例14: appendToFile
import java.util.logging.FileHandler; //导入方法依赖的package包/类
/**
* Helper that configures java.util.logging to sanely log messages at FINE level without additional
* formatting.
*/
public JavaLogger appendToFile(String logfile) {
logger.setLevel(java.util.logging.Level.FINE);
try {
FileHandler handler = new FileHandler(logfile, true);
handler.setFormatter(new SimpleFormatter() {
@Override
public String format(LogRecord record) {
return String.format("%s%n", record.getMessage()); // NOPMD
}
});
logger.addHandler(handler);
} catch (IOException e) {
throw new IllegalStateException("Could not add file handler.", e);
}
return this;
}
示例15: initLogger
import java.util.logging.FileHandler; //导入方法依赖的package包/类
private static void initLogger() {
if (Files.notExists(Paths.get("logs\\"))) {
new File("logs\\").mkdir();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");
try {
fh = new FileHandler("logs\\BTB-Logger_" + sdf.format(new Date()) + ".log", false);
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
Logger l = Logger.getLogger("");
fh.setFormatter(new SimpleFormatter());
l.addHandler(fh);
l.setLevel(Level.CONFIG);
logger.log(Level.INFO, "Logger initialized");
}