Example usage for java.util.logging Logger log

List of usage examples for java.util.logging Logger log

Introduction

In this page you can find the example usage for java.util.logging Logger log.

Prototype

public void log(Level level, Supplier<String> msgSupplier) 

Source Link

Document

Log a message, which is only to be constructed if the logging level is such that the message will actually be logged.

Usage

From source file:MyLevel.java

public static void main(String[] argv) throws Exception {
    Logger logger = Logger.getLogger("com.mycompany");
    logger.log(MyLevel.DISASTER, "my disaster message");

    Level disaster = Level.parse("DISASTER");
    logger.log(disaster, "my disaster message");
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    LogManager lm = LogManager.getLogManager();
    Logger logger;
    FileHandler fh = new FileHandler("log_test.txt");

    logger = Logger.getLogger("LoggingExample1");

    lm.addLogger(logger);//from ww w.j  a v  a2  s  .c om
    logger.setLevel(Level.INFO);
    fh.setFormatter(new XMLFormatter());

    logger.addHandler(fh);
    //logger.setUseParentHandlers(false);
    logger.log(Level.INFO, "test 1");
    logger.log(Level.INFO, "test 2");
    logger.log(Level.INFO, "test 3");
    fh.close();
}

From source file:LoggingExample1.java

public static void main(String args[]) {
    try {/* w ww. j  ava  2  s  .c om*/
        LogManager lm = LogManager.getLogManager();
        Logger logger;
        FileHandler fh = new FileHandler("log_test.txt");

        logger = Logger.getLogger("LoggingExample1");

        lm.addLogger(logger);
        logger.setLevel(Level.INFO);
        fh.setFormatter(new XMLFormatter());

        logger.addHandler(fh);
        //logger.setUseParentHandlers(false);
        logger.log(Level.INFO, "test 1");
        logger.log(Level.INFO, "test 2");
        logger.log(Level.INFO, "test 3");
        fh.close();
    } catch (Exception e) {
        System.out.println("Exception thrown: " + e);
        e.printStackTrace();
    }
}

From source file:bluevia.examples.MODemo.java

/**
 * @param args//  w w w. j av  a 2  s.  com
 */
public static void main(String[] args) throws IOException {

    BufferedReader iReader = null;
    String apiDataFile = "API-AccessToken.ini";

    String consumer_key;
    String consumer_secret;
    String registrationId;

    OAuthConsumer apiConsumer = null;
    HttpURLConnection request = null;
    URL moAPIurl = null;

    Logger logger = Logger.getLogger("moSMSDemo.class");
    int i = 0;
    int rc = 0;

    Thread mThread = Thread.currentThread();

    try {
        System.setProperty("debug", "1");

        iReader = new BufferedReader(new FileReader(apiDataFile));

        // Private data: consumer info + access token info + phone info
        consumer_key = iReader.readLine();
        consumer_secret = iReader.readLine();
        registrationId = iReader.readLine();

        // Set up the oAuthConsumer
        while (true) {
            try {

                logger.log(Level.INFO, String.format("#%d: %s\n", ++i, "Requesting messages..."));

                apiConsumer = new DefaultOAuthConsumer(consumer_key, consumer_secret);

                apiConsumer.setMessageSigner(new HmacSha1MessageSigner());

                moAPIurl = new URL("https://api.bluevia.com/services/REST/SMS/inbound/" + registrationId
                        + "/messages?version=v1&alt=json");

                request = (HttpURLConnection) moAPIurl.openConnection();
                request.setRequestMethod("GET");

                apiConsumer.sign(request);

                StringBuffer doc = new StringBuffer();
                BufferedReader br = null;

                rc = request.getResponseCode();
                if (rc == HttpURLConnection.HTTP_OK) {
                    br = new BufferedReader(new InputStreamReader(request.getInputStream()));

                    String line = br.readLine();
                    while (line != null) {
                        doc.append(line);
                        line = br.readLine();
                    }

                    System.out.printf("Output message: %s\n", doc.toString());
                    try {
                        JSONObject apiResponse1 = new JSONObject(doc.toString());
                        String aux = apiResponse1.getString("receivedSMS");
                        if (aux != null) {
                            String szMessage;
                            String szOrigin;
                            String szDate;

                            JSONObject smsPool = apiResponse1.getJSONObject("receivedSMS");
                            JSONArray smsInfo = smsPool.optJSONArray("receivedSMS");
                            if (smsInfo != null) {
                                for (i = 0; i < smsInfo.length(); i++) {
                                    szMessage = smsInfo.getJSONObject(i).getString("message");
                                    szOrigin = smsInfo.getJSONObject(i).getJSONObject("originAddress")
                                            .getString("phoneNumber");
                                    szDate = smsInfo.getJSONObject(i).getString("dateTime");
                                    System.out.printf("#%d %s\n - from %s\n - message:%s\n", i, szDate,
                                            szOrigin, szMessage);
                                }
                            } else {
                                JSONObject sms = smsPool.getJSONObject("receivedSMS");
                                szMessage = sms.getString("message");
                                szOrigin = sms.getJSONObject("originAddress").getString("phoneNumber");
                                szDate = sms.getString("dateTime");
                                System.out.printf("#%d %s\n - from %s\n - message:%s\n", i, szDate, szOrigin,
                                        szMessage);
                            }
                        }
                    } catch (JSONException e) {
                        System.err.println("JSON error: " + e.getMessage());
                    }

                } else if (rc == HttpURLConnection.HTTP_NO_CONTENT)
                    System.out.printf("No content\n");
                else
                    System.err.printf("Error: %d:%s\n", rc, request.getResponseMessage());

                request.disconnect();

            } catch (Exception e) {
                System.err.println("Exception: " + e.getMessage());
            }
            mThread.sleep(15000);
        }
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
    }
}

From source file:com.bigtobster.pgnextractalt.commands.CommandContext.java

/**
 * Logs a severe error in logs/*from w  w  w .j a  v a  2  s.  co m*/
 *
 * @param logger    The erring class's Logger
 * @param message   The message to be logged
 * @param exception The exception causing the error
 */
@SuppressWarnings("SameParameterValue")
private static void logSevereError(@SuppressWarnings("SameParameterValue") final Logger logger,
        final String message, final Exception exception) {
    logger.log(Level.SEVERE, message);
    logger.log(Level.SEVERE, exception.getMessage());
    logger.log(Level.SEVERE, exception.toString(), exception.fillInStackTrace());
}

From source file:Main.java

/**
 * This method recursively visits (log.info()) all thread groups under `group'.
 * //ww w . j  a v  a 2s. co  m
 * @param log
 * @param logLevel
 */
public static void logThreadGroup(Logger log, Level logLevel, ThreadGroup group, int level) {
    // Get threads in `group'
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);

    // Enumerate each thread in `group'
    for (int i = 0; i < numThreads; i++) {
        // Get thread/
        Thread thread = threads[i];
        log.log(logLevel, thread.toString());
    }

    // Get thread subgroups of `group'
    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);

    // Recursively visit each subgroup
    for (int i = 0; i < numGroups; i++) {
        logThreadGroup(log, logLevel, groups[i], level + 1);
    }
}

From source file:at.pcgamingfreaks.Bukkit.Utils.java

/**
 * Converts an item stack into a json string used for chat messages.
 *
 * @param itemStack The item stack that should be converted into a json string
 * @param logger The logger that should display the error message in case of an problem
 * @return The item stack as a json string. empty string if the conversation failed
 *//*from  ww w.ja v a 2s.co  m*/
public static String convertItemStackToJson(@NotNull ItemStack itemStack, @NotNull Logger logger) {
    Validate.notNull(logger, "The logger can't be null.");
    Validate.notNull(itemStack, "The item stack can't be null.");
    try {
        if (SAVE_NMS_ITEM_STACK_METHOD == null || AS_NMS_COPY_METHOD == null
                || NBT_TAG_COMPOUND_CLASS == null) {
            logger.log(Level.SEVERE, "Failed to serialize item stack to NMS item! Bukkit Version: "
                    + Bukkit.getServer().getVersion()
                    + "\nOne or more of the reflection variables is null! Looks like your bukkit version is not compatible. Please check for updates.");
        } else {
            return SAVE_NMS_ITEM_STACK_METHOD
                    .invoke(AS_NMS_COPY_METHOD.invoke(null, itemStack), NBT_TAG_COMPOUND_CLASS.newInstance())
                    .toString();
        }
    } catch (Throwable t) {
        logger.log(Level.SEVERE, "Failed to serialize item stack to NMS item! Bukkit Version: "
                + Bukkit.getServer().getVersion() + "\n", t);
    }
    return "";
}

From source file:pcgen.util.Logging.java

/**
 * Log a message, if logging is enabled at the
 * supplied level of detail. //from  w  ww . j a v  a  2s . c o  m
 * 
 * @param lvl The detail level of the message
 * @param msg String message
 */
public static void log(final Level lvl, final String msg) {
    Logger l = getLogger();
    if (l.isLoggable(lvl)) {
        l.log(lvl, msg);
    }
}

From source file:pcgen.util.Logging.java

/**
 * Print information message if PCGen is debugging.
 *
 * @param param1 String information message (usually variable)
 * @param param2 Object information message (usually value)
 */// w ww  . j a  v a2s.  com
public static void debugPrint(final String param1, Object param2) {
    Logger l = getLogger();
    if (l.isLoggable(DEBUG)) {
        l.log(DEBUG, param1 + param2);
    }
}

From source file:pcgen.util.Logging.java

/**
 * Beep and print error message if PCGen is debugging.
 *
 * @param s String error message/*  www .j a  v a  2s  .co  m*/
 */
public static void errorPrint(final String s) {
    if (debugMode) {
        S_TOOLKIT.beep();
    }

    Logger l = getLogger();
    if (l.isLoggable(ERROR)) {
        l.log(ERROR, s);
    }
}