Created
August 7, 2017 22:56
-
-
Save mcSw4p/4dba63efed94ee0194b9ede810d19bec to your computer and use it in GitHub Desktop.
A java class that shows how to format a Logger.class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.wynsolutions.slackapi.log; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.logging.Formatter; | |
import java.util.logging.Handler; | |
import java.util.logging.LogRecord; | |
/** | |
* Copyright (C) 2017 Sw4p | |
* | |
* This program is free software: you can redistribute it and/or modify it under | |
* the terms of the GNU General Public License as published by the Free Software | |
* Foundation, either version 3 of the License, or (at your option) any later | |
* version. | |
* | |
* This program is distributed in the hope that it will be useful, but WITHOUT | |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
* details. | |
* | |
* You should have received a copy of the GNU General Public License along with | |
* this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* @author Sw4p | |
* | |
*/ | |
public class LoggerFormatter extends Formatter{ | |
// Create a DateFormat to format the logger timestamp. | |
private static final DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS"); | |
public String format(LogRecord record) { | |
StringBuilder builder = new StringBuilder(1000); | |
builder.append("[").append(df.format(new Date(record.getMillis()))).append("] "); | |
builder.append("[").append(record.getLevel()).append("] "); | |
builder.append(formatMessage(record)); | |
builder.append("\n"); | |
return builder.toString(); | |
} | |
public String getHead(Handler h) { | |
return super.getHead(h); | |
} | |
public String getTail(Handler h) { | |
return super.getTail(h); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.wynsolutions.slackapi; | |
import java.util.logging.ConsoleHandler; | |
import java.util.logging.Logger; | |
import net.wynsolutions.slackapi.log.LoggerFormatter; | |
/** | |
* Copyright (C) 2017 Sw4p | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* @author Sw4p | |
* | |
*/ | |
public class Main { | |
public static void main(String[] args){ | |
Logger logger = Logger.getLogger(Main.class.toString()); | |
logger.setUseParentHandlers(false); | |
ConsoleHandler handler = new ConsoleHandler(); | |
handler.setFormatter(new LoggerFormatter()); | |
logger.addHandler(handler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment