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(); } }
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()); } } }
/** * 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; }
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); }
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); }
@Before public void setupLogger() throws Exception { Level level = Level.SEVERE; Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.err.println("uncaughtException from thread " + t.getName() + ": " + e); e.printStackTrace(); } }); java.util.logging.LogManager.getLogManager().reset(); ConsoleHandler ch = new ConsoleHandler(); ch.setLevel(level); SimpleFormatter f = new SimpleFormatter(); ch.setFormatter(f); java.util.logging.Logger.getLogger("").setLevel(level); java.util.logging.Logger.getLogger("").addHandler(ch); }
@Before public void setupLogger() throws Exception { Level level = Level.INFO; Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.err.println("uncaughtException from thread " + t.getName() + ": " + e); e.printStackTrace(); } }); java.util.logging.LogManager.getLogManager().reset(); ConsoleHandler ch = new ConsoleHandler(); ch.setLevel(level); SimpleFormatter f = new SimpleFormatter(); ch.setFormatter(f); java.util.logging.Logger.getLogger("").setLevel(level); java.util.logging.Logger.getLogger("").addHandler(ch); }
public void testConstructor_InvalidProperties() throws Exception { Properties p = new Properties(); p.put("java.util.logging.ConsoleHandler.level", INVALID_LEVEL); p.put("java.util.logging.ConsoleHandler.filter", className); p.put("java.util.logging.ConsoleHandler.formatter", className); p.put("java.util.logging.ConsoleHandler.encoding", "XXXX"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); assertEquals(LogManager.getLogManager().getProperty( "java.util.logging.ConsoleHandler.level"), INVALID_LEVEL); assertEquals(LogManager.getLogManager().getProperty( "java.util.logging.ConsoleHandler.encoding"), "XXXX"); ConsoleHandler h = new ConsoleHandler(); assertSame(h.getLevel(), Level.INFO); assertTrue(h.getFormatter() instanceof SimpleFormatter); assertNull(h.getFilter()); assertNull(h.getEncoding()); h.publish(new LogRecord(Level.SEVERE, "test")); assertNull(h.getEncoding()); }
private static void enableHttp2FrameLogging() { frameLogger = Logger.getLogger(Http2.class.getName()); frameLogger.setLevel(Level.FINE); ConsoleHandler handler = new ConsoleHandler(); handler.setLevel(Level.FINE); handler.setFormatter(new SimpleFormatter() { @Override public String format(LogRecord record) { return Util.format("%s%n", record.getMessage()); } }); frameLogger.addHandler(handler); }
public LoggingConfig() { try { // Load a properties file from class path java.util.logging.config.file final LogManager logManager = LogManager.getLogManager(); URL configURL = getClass().getResource("/logging.properties"); if (configURL != null) { try (InputStream is = configURL.openStream()) { logManager.readConfiguration(is); } } else { // Programmatic configuration System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] %5$s %6$s%n"); final ConsoleHandler consoleHandler = new ConsoleHandler(); consoleHandler.setLevel(Level.FINEST); consoleHandler.setFormatter(new SimpleFormatter()); final Logger app = Logger.getLogger("app"); app.setLevel(Level.FINEST); app.addHandler(consoleHandler); } } catch (Exception e) { e.printStackTrace(); } }
/** * Constructor. */ public ServerRunner() { log.setLevel(Level.ALL); ConsoleHandler handler = new ConsoleHandler(); handler.setFormatter(new SimpleFormatter()); handler.setLevel(Level.ALL); log.addHandler(handler); }
protected static Logger createLogger() { // Initialize logging StreamHandler handler = new StreamHandler(System.out, new SimpleFormatter()); handler.setLevel(Level.ALL); Logger logger = Logger.getLogger(GeFLoRegistry.class.getName()); logger.addHandler(handler); /* * Change the level if you want to... * - disable logging to Level.OFF * - enable logging to Level.ALL */ logger.setLevel(Level.ALL); return logger; }
/** * Creates a static {@code Logger} instance. * * @return a static {@code Logger} with properties: * <ul> * <li>Name: {@code "DefaultDatabaseConfigurator"}.</li> * <li>Output file pattern: * {@code user.home/.kawansoft/log/AceQL.log}.</li> * <li>Formatter: {@code SimpleFormatter}.</li> * <li>Limit: 200Mb.</li> * <li>Count (number of files to use): 2.</li> * </ul> */ @Override public Logger getLogger() throws IOException { if (ACEQL_LOGGER != null) { return ACEQL_LOGGER; } File logDir = new File(SystemUtils.USER_HOME + File.separator + ".kawansoft" + File.separator + "log"); logDir.mkdirs(); String pattern = logDir.toString() + File.separator + "AceQL.log"; ACEQL_LOGGER = Logger.getLogger(DefaultDatabaseConfigurator.class.getName()); Handler fh = new FileHandler(pattern, 200 * 1024 * 1024, 2, true); fh.setFormatter(new SimpleFormatter()); ACEQL_LOGGER.addHandler(fh); return ACEQL_LOGGER; }
/** * 初始化日志记录 */ 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); } }
@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); } }
/** * 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; }
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"); }
private static void handleAnnouncements(LiveEvent event, JsonObject payload) { try { if (Files.notExists(Paths.get("payloads\\"))) { new File("payloads\\").mkdir(); } Logger logger = Logger.getLogger("payload-announcement"); FileHandler fh = new FileHandler("payloads\\" + logger.getName() + ".json"); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); logger.addHandler(fh); logger.log(Level.INFO, payload.toString()); } catch (SecurityException | IOException e) {} }
private static void handleChannelFollowed(LiveEvent event, JsonObject payload) { try { if (Files.notExists(Paths.get("payloads\\"))) { new File("payloads\\").mkdir(); } Logger logger = Logger.getLogger("payload-channelFollowed"); FileHandler fh = new FileHandler("payloads\\" + logger.getName() + ".json"); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); logger.addHandler(fh); logger.log(Level.INFO, payload.toString()); } catch (SecurityException | IOException e) {} }
private static void handleUserFollowed(LiveEvent event, JsonObject payload) { try { if (Files.notExists(Paths.get("payloads\\"))) { new File("payloads\\").mkdir(); } Logger logger = Logger.getLogger("payload-userFollowed"); FileHandler fh = new FileHandler("payloads\\" + logger.getName() + ".json"); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); logger.addHandler(fh); logger.log(Level.INFO, payload.toString()); } catch (SecurityException | IOException e) {} }
/** * Deserializes the Base64 encoded string returned by {@link * #getBase64()}, format the obtained LogRecord using a * SimpleFormatter, and checks that the string representation obtained * matches the original string representation returned by {@link * #getString()}. */ protected void dotest() { try { final String base64 = getBase64(); final ByteArrayInputStream bais = new ByteArrayInputStream(Base64.getDecoder().decode(base64)); final ObjectInputStream ois = new ObjectInputStream(bais); final LogRecord record = (LogRecord)ois.readObject(); final SimpleFormatter formatter = new SimpleFormatter(); String expected = getString(); String str2 = formatter.format(record); check(expected, str2); System.out.println(str2); System.out.println("PASSED: "+this.getClass().getName()+"\n"); } catch (IOException | ClassNotFoundException x) { throw new RuntimeException(x); } }
private static void enableHttp2FrameLogging() { Logger logger = Logger.getLogger(Http2.class.getName() + "$FrameLogger"); logger.setLevel(Level.FINE); ConsoleHandler handler = new ConsoleHandler(); handler.setLevel(Level.FINE); handler.setFormatter(new SimpleFormatter() { @Override public String format(LogRecord record) { return String.format("%s%n", record.getMessage()); } }); logger.addHandler(handler); }
public OutLogger(String name, boolean stateOnly) { super(name); _name = name; _stateOnly = stateOnly; Logger logger = Logger.getLogger(name); if (!isNew(logger)) return; Logger.getLogger(name).setLevel(Level.ALL); ConsoleHandler console = new ConsoleHandler(); console.setLevel(Level.ALL); console.setFormatter(new SimpleFormatter() { public synchronized String format(LogRecord record) { return System.currentTimeMillis() + " (" + _name + ") " + record.getMessage() + "\r\n"; } }); Logger.getLogger(name).addHandler(console); }
public void configureHandler( Level baseLevel ) { Handler[] hList = LogManager.getLogManager().getLogger( "" ).getHandlers(); for ( Handler h : hList ) { if ( h instanceof ThreadedFileHandler ) return; } setLevel( baseLevel ); setFormatter( new SimpleFormatter() ); LogManager.getLogManager().getLogger( "" ).addHandler( this ); }
private void testLogger( StreamHandler afh ) { Handler[] handlers = LOGGER.getHandlers(); for ( Handler handler : handlers ) { try { LOGGER.removeHandler( handler ); } catch ( Exception e ) { e.printStackTrace(); } } afh.setFormatter( new SimpleFormatter() ); afh.setLevel( Level.FINEST ); LOGGER.addHandler( afh ); LOGGER.setLevel( Level.FINEST ); final int threadCount = 20; final int logEntryCount = 100000; generateLogs( threadCount, logEntryCount ); afh.close(); }