Example usage for java.util.logging Logger isLoggable

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

Introduction

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

Prototype

public boolean isLoggable(Level level) 

Source Link

Document

Check if a message of the given level would actually be logged by this logger.

Usage

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandler.java

private Level getLogLevel(final Logger logger) {
    final Level detailed = TraceLevel.DETAILED.getLevel();
    final Level normal = TraceLevel.SIMPLE.getLevel();
    final boolean detailedLoggable = logger.isLoggable(detailed);
    final boolean normalLoggable = logger.isLoggable(normal);

    return detailedLoggable ? detailed : normalLoggable ? normal : null;
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

/**
 * Log a tax error/*from w  ww.  j  a  v a2  s. c o m*/
 */
@Override
public void logAccountFeeError(final AccountFeeLog feeLog, final Throwable error) {
    final Logger logger = getAccountFeeLogger();
    final Level level = AccountFeeLevel.ERRORS.getLevel();
    if (logger.isLoggable(level)) {
        try {
            logger.log(level, "Error on " + feeLog.getAccountFee().getName(), error);
        } catch (final Exception e) {
            System.out
                    .println("Error generating log on " + settingsService.getLogSettings().getAccountFeeFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

@Override
public void logAccountFeeFinished(final AccountFeeLog feeLog) {
    final Logger logger = getAccountFeeLogger();
    final Level level = AccountFeeLevel.STATUS.getLevel();
    if (logger.isLoggable(level)) {
        try {/*  w w w.j  a v a  2s. c o m*/
            logger.log(level, feeLog.getAccountFee().getName() + ": charging has finished");
        } catch (final Exception e) {
            System.out
                    .println("Error generating log on " + settingsService.getLogSettings().getAccountFeeFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

/**
 * Log an account fee transfer/*from   ww  w. ja  v  a2 s.c o  m*/
 */
@Override
public void logAccountFeeInvoice(final Invoice invoice) {
    final Logger logger = getAccountFeeLogger();
    final Level level = AccountFeeLevel.DETAILED.getLevel();
    if (logger.isLoggable(level)) {
        final UnitsConverter unitsConverter = settingsService.getLocalSettings()
                .getUnitsConverter(invoice.getTransferType().getFrom().getCurrency().getPattern());
        final String message = "Sent invoice of %s from %s";
        final Object[] params = { unitsConverter.toString(invoice.getAmount()),
                invoice.getToMember().getUsername() };
        try {
            logger.log(level, String.format(message, params));
        } catch (final Exception e) {
            System.out
                    .println("Error generating log on " + settingsService.getLogSettings().getAccountFeeFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

/**
 * Log an account fee transfer/*from   w  ww.j a va  2  s.c  o  m*/
 */
@Override
public void logAccountFeePayment(final Transfer transfer) {
    final Logger logger = getAccountFeeLogger();
    final Level level = AccountFeeLevel.DETAILED.getLevel();
    if (logger.isLoggable(level)) {
        final AccountFeeLog feeLog = transfer.getAccountFeeLog();
        final AccountFee fee = feeLog.getAccountFee();
        final UnitsConverter unitsConverter = settingsService.getLocalSettings()
                .getUnitsConverter(transfer.getFrom().getType().getCurrency().getPattern());
        String message;
        Object[] params;
        if (fee.getPaymentDirection() == PaymentDirection.TO_SYSTEM) {
            message = "Charged %s from %s";
            params = new Object[] { unitsConverter.toString(transfer.getAmount()),
                    transfer.getFrom().getOwnerName() };
        } else {
            message = "Paid %s to %s";
            params = new Object[] { unitsConverter.toString(transfer.getAmount()),
                    transfer.getTo().getOwnerName() };
        }
        try {
            logger.log(level, String.format(message, params));
        } catch (final Exception e) {
            System.out
                    .println("Error generating log on " + settingsService.getLogSettings().getAccountFeeFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

@Override
public void logAccountFeeStarted(final AccountFeeLog feeLog) {
    final Logger logger = getAccountFeeLogger();
    final Level level = AccountFeeLevel.STATUS.getLevel();
    if (logger.isLoggable(level)) {
        try {//from w w w.j av  a2  s .  co m
            logger.log(level, feeLog.getAccountFee().getName() + ": charging has started");
        } catch (final Exception e) {
            System.out
                    .println("Error generating log on " + settingsService.getLogSettings().getAccountFeeFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

@Override
public void logScheduledTaskError(final String taskName, final Calendar hour, final Exception error) {
    final Logger logger = getScheduledTaskLogger();
    final Level level = ScheduledTaskLevel.ERRORS.getLevel();
    if (logger.isLoggable(level)) {
        try {/*from   w w w  .  ja v a 2s .c  o m*/
            logger.log(level,
                    "Exception on scheduled task: " + taskName + " for hour " + FormatObject.formatObject(hour),
                    error);
        } catch (final Exception e) {
            System.out.println(
                    "Error generating log on " + settingsService.getLogSettings().getScheduledTaskFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

@Override
public void logScheduledTaskTrace(final String taskName, final Calendar hour, final long timeTaken) {
    final Logger logger = getScheduledTaskLogger();
    final Level level = ScheduledTaskLevel.DETAILED.getLevel();
    if (logger.isLoggable(level)) {
        final MathContext mathContext = settingsService.getLocalSettings().getMathContext();
        final String formattedTime = settingsService.getLocalSettings().getNumberConverter()
                .toString(new BigDecimal(timeTaken).divide(new BigDecimal(1000), mathContext));
        try {/*from  w  w w  .  j av a  2 s .  c  om*/
            logger.log(level, String.format("Scheduled task '%s' for hour %s ran on %s seconds", taskName,
                    FormatObject.formatObject(hour), formattedTime));
        } catch (final Exception e) {
            System.out.println(
                    "Error generating log on " + settingsService.getLogSettings().getScheduledTaskFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

@Override
public void logSchedulingTrace(final Calendar hour, final long timeTaken) {
    final Logger logger = getScheduledTaskLogger();
    final Level level = ScheduledTaskLevel.INFO.getLevel();
    if (logger.isLoggable(level)) {
        final MathContext mathContext = settingsService.getLocalSettings().getMathContext();
        final String formattedTime = settingsService.getLocalSettings().getNumberConverter()
                .toString(new BigDecimal(timeTaken).divide(new BigDecimal(1000), mathContext));
        try {//from  w w  w.  ja v a2  s  . co  m
            logger.log(level, String.format("Scheduled tasks for hour %s ran on %s seconds",
                    FormatObject.formatObject(hour), formattedTime));
        } catch (final Exception e) {
            System.out.println(
                    "Error generating log on " + settingsService.getLogSettings().getScheduledTaskFile());
        }
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandlerImpl.java

/**
 * Log a successful transfer// w w w.  j  a va2  s. c o m
 */
@Override
public void logTransfer(final Transfer transfer) {
    final Logger logger = getTransactionLogger();
    final Level detailed = TransactionLevel.DETAILED.getLevel();
    final Level normal = TransactionLevel.NORMAL.getLevel();
    final boolean detailedLoggable = logger.isLoggable(detailed);
    final boolean normalLoggable = logger.isLoggable(normal);
    final boolean willLog = detailedLoggable || normalLoggable;
    // Generate log if, at least, normal level is enabled
    if (willLog) {
        // transfer = fetchService.fetch(transfer, RelationshipHelper.nested(Payment.Relationships.FROM, Account.Relationships.TYPE,
        // AccountType.Relationships.CURRENCY), Payment.Relationships.TO);
        Level level;
        final LocalSettings localSettings = settingsService.getLocalSettings();
        final UnitsConverter unitsConverter = localSettings
                .getUnitsConverter(transfer.getFrom().getType().getCurrency().getPattern());
        String message;
        Object[] args;
        // Get the specific level arguments
        String loggedUser = LoggedUser.hasUser() ? LoggedUser.user().getUsername() : "<no logged user>";
        if (detailedLoggable) {
            final TransferType type = transfer.getType();
            level = detailed;
            message = "logged user: %s, id: %s, date: %s, type: %s (%s), amount: %s, from: %s, to: %s, by: %s, tx#: %s, description: %s";
            final Element by = transfer.getBy();
            args = new Object[] { loggedUser, transfer.getId(),
                    localSettings.getDateTimeConverter().toString(transfer.getDate()), type.getId(),
                    type.getName(), unitsConverter.toString(transfer.getAmount()),
                    transfer.getFrom().getOwnerName(), transfer.getTo().getOwnerName(),
                    by == null ? "<null>" : by.getUsername(),
                    StringUtils.defaultIfEmpty(transfer.getTransactionNumber(), "<null>"),
                    StringUtils.replace(transfer.getDescription(), "\n", "\\n") };
        } else {
            level = normal;
            message = "logged user: %s, id: %s, amount: %s, from: %s, to: %s";
            args = new Object[] { loggedUser, transfer.getId(), unitsConverter.toString(transfer.getAmount()),
                    transfer.getFrom().getOwnerName(), transfer.getTo().getOwnerName() };
        }
        try {
            logger.log(level, String.format(message, args));
        } catch (final Exception e) {
            System.out.println(
                    "Error generating log on " + settingsService.getLogSettings().getTransactionFile());
        }
    }
}