com.jcabi.log.Logger类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中com.jcabi.log.Logger类的一些代码示例,展示了Logger类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Logger类的具体详情如下:
包路径:com.jcabi.log.Logger
类名称:Logger

Logger介绍

[英]Universal logger, and adapter between your app and SLF4J API.

Instead of relying on some logging engine you can use this class, which transforms all messages to SLF4J. This approach gives you a perfect decoupling of business logic and logging mechanism. All methods in the class are called statically, without the necessity to instantiate the class.

Use it like this in any class, and in any package:

package com.example.XXX; 
import com.jcabi.log.Logger; 
public class MyClass { 
public void foo(Integer num) { 
Logger.info(this, "foo(%d) just called", num); 
} 
}

Or statically (pay attention to MyClass.class):

public class MyClass { 
public static void foo(Integer num) { 
Logger.info(MyClass.class, "foo(%d) just called", num); 
} 
}

Exact binding between SLF4J and logging facility has to be specified in pom.xml of your project (or in classpath directly).

For performance reasons in most cases before sending a TRACE or DEBUG log message you may check whether this logging level is enabled in the project, e.g.:

//... 
if (Logger.isTraceEnabled(this)) { 
Logger.trace(this, "#foo() called"); 
} 
//...

There is only one reason to do so - if you want to save time spent on preparing of the arguments. By default, such a call is made inside every method of Logger class.
[中]通用记录器,以及应用程序和SLF4J API之间的适配器。
您可以使用这个类,将所有消息转换为SLF4J,而不是依赖于某些日志引擎。这种方法为您提供了业务逻辑和日志机制的完美解耦。类中的所有方法都是静态调用的,无需实例化类。
在任何类和任何包中都这样使用它:

package com.example.XXX; 
import com.jcabi.log.Logger; 
public class MyClass { 
public void foo(Integer num) { 
Logger.info(this, "foo(%d) just called", num); 
} 
}

或者静态地(注意MyClass.class):

public class MyClass { 
public static void foo(Integer num) { 
Logger.info(MyClass.class, "foo(%d) just called", num); 
} 
}

必须在pom中规定SLF4J和测井设施之间的精确绑定。项目的xml(或直接在类路径中)。
出于性能原因,在大多数情况下,在发送跟踪或调试日志消息之前,您可以检查项目中是否启用了此日志记录级别,例如:

//... 
if (Logger.isTraceEnabled(this)) { 
Logger.trace(this, "#foo() called"); 
} 
//...

这样做的原因只有一个——如果您想节省准备参数的时间。默认情况下,这样的调用在Logger类的每个方法中进行。

代码示例

代码示例来源:origin: jcabi/jcabi-aspects

/**
 * Log one line.
 * @param level Level of logging
 * @param log Destination log
 * @param message Message to log
 * @param params Message parameters
 * @checkstyle ParameterNumberCheck (3 lines)
 */
public static void log(final int level, final Object log,
  final String message, final Object... params) {
  if (level == Loggable.TRACE) {
    Logger.trace(log, message, params);
  } else if (level == Loggable.DEBUG) {
    Logger.debug(log, message, params);
  } else if (level == Loggable.INFO) {
    Logger.info(log, message, params);
  } else if (level == Loggable.WARN) {
    Logger.warn(log, message, params);
  } else if (level == Loggable.ERROR) {
    Logger.error(log, message, params);
  }
}

代码示例来源:origin: com.jcabi/jcabi-log

/**
 * Protocol one message, with {@code ERROR} priority level.
 * @param source The source of the logging operation
 * @param msg The text message to be logged, with meta-tags
 * @param args List of arguments
 */
public static void error(final Object source,
  final String msg, final Object... args) {
  Logger.logger(source).error(Logger.format(msg, args));
}

代码示例来源:origin: com.rexsl/rexsl

@Override
  @NotNull
  public String render(@NotNull final String defect) {
    Logger.warn(this, "#render(..): %s", this.message);
    return Logger.format(
      "<html><body><pre>%s\n\n%s</pre></body></html>",
      this.message,
      defect
    );
  }
}

代码示例来源:origin: com.jcabi/jcabi-log

/**
 * Protocol one message, with {@code INFO} priority level.
 * @param source The source of the logging operation
 * @param msg The text message to be logged
 * @since 0.7.11
 */
public static void info(final Object source, final String msg) {
  Logger.info(source, msg, Logger.EMPTY);
}

代码示例来源:origin: com.jcabi/jcabi-log

/**
 * Protocol one message, with {@code WARN} priority level.
 * @param source The source of the logging operation
 * @param msg The text message to be logged
 * @since 0.7.11
 */
public static void warn(final Object source, final String msg) {
  Logger.warn(source, msg, Logger.EMPTY);
}

代码示例来源:origin: com.jcabi/jcabi-log

/**
 * Protocol one message, with {@code DEBUG} priority level.
 * @param source The source of the logging operation
 * @param msg The text message to be logged, with meta-tags
 * @since 0.7.11
 */
public static void debug(final Object source, final String msg) {
  Logger.debug(source, msg, Logger.EMPTY);
}

代码示例来源:origin: com.jcabi/jcabi-w3c

@Override
public String toString() {
  return Logger.format(
    "[%d:%d] \"%s\", \"%s\", \"%s\", \"%s\"",
    this.iline,
    this.icolumn,
    this.isource,
    this.iexplanation,
    this.msg,
    this.imessage
  );
}

代码示例来源:origin: jcabi/jcabi-log

/**
 * Protocol one message, with {@code ERROR} priority level.
 * @param source The source of the logging operation
 * @param msg The text message to be logged
 * @since 0.7.11
 */
public static void error(final Object source, final String msg) {
  Logger.error(source, msg, Logger.EMPTY);
}

代码示例来源:origin: Cognifide/aet

private void downloadXUnitTest(String xUnitUrl) {
 try {
  String xUnitFullUrl = endpointDomain + xUnitUrl;
  Logger.debug(this, "XUnit report URL: '%s'", xUnitFullUrl);
  new ReportWriter().write(buildDirectory, xUnitFullUrl, "xunit-report.xml");
 } catch (IOException ioe) {
  Logger.error(this, "Failed to obtain xUnit report from: %s. Error: %s", xUnitUrl,
    ioe.getMessage());
 }
}

代码示例来源:origin: com.jcabi/jcabi-log

@Override
  @SuppressWarnings("PMD.AvoidCatchingGenericException")
  public void run() {
    try {
      this.origin.run();
      // @checkstyle IllegalCatch (1 line)
    } catch (final RuntimeException ex) {
      Logger.warn(
        this,
        "%s: %[exception]s",
        Thread.currentThread().getName(),
        ex
      );
      throw ex;
      // @checkstyle IllegalCatch (1 line)
    } catch (final Error error) {
      Logger.error(
        this,
        "%s (error): %[exception]s",
        Thread.currentThread().getName(),
        error
      );
      throw error;
    }
  }
}

代码示例来源:origin: jcabi/jcabi-aspects

this.point.getSignature()
).getMethod();
Logger.warn(
  method.getDeclaringClass(),
  "%s: takes more than %[ms]s, %[ms]s already, thread=%s/%s",
  this.thread.getState()
);
Logger.debug(
  method.getDeclaringClass(),
  "%s: thread %s/%s stacktrace: %s",

代码示例来源:origin: jcabi/jcabi-log

/**
 * Protocol one message, with {@code INFO} priority level.
 * @param source The source of the logging operation
 * @param msg The text message to be logged
 * @since 0.7.11
 */
public static void info(final Object source, final String msg) {
  Logger.info(source, msg, Logger.EMPTY);
}

代码示例来源:origin: com.jcabi/jcabi-log

@Override
  public void uncaughtException(final Thread thread,
    final Throwable throwable) {
    Logger.warn(this, "%[exception]s", throwable);
  }
}

代码示例来源:origin: com.rexsl/rexsl

/**
 * Public ctor.
 * @param response The resource
 * @param log Log message
 */
public AuthException(final Response response, final String log) {
  super(response);
  Logger.debug(AuthException.class, "%s", log);
}

代码示例来源:origin: com.rexsl/rexsl

/**
   * Asserts that an object is not <code>null</code> and throws
   * IllegalStateException if the object is <code>null</code>.
   * @param object The object to check
   * @param message The exception message to use if the assertion fails
   * @since 0.12
   */
  private void assertNotNull(final Object object, final String message) {
    if (object == null) {
      throw new IllegalStateException(Logger.format(message, this));
    }
  }
}

代码示例来源:origin: com.jcabi/jcabi-log

/**
 * Protocol one message, with {@code ERROR} priority level.
 * @param source The source of the logging operation
 * @param msg The text message to be logged
 * @since 0.7.11
 */
public static void error(final Object source, final String msg) {
  Logger.error(source, msg, Logger.EMPTY);
}

代码示例来源:origin: com.cognifide.aet/client-core

private void downloadXUnitTest(String xUnitUrl) {
 try {
  String xUnitFullUrl = endpointDomain + xUnitUrl;
  Logger.debug(this, "XUnit report URL: '%s'", xUnitFullUrl);
  new ReportWriter().write(buildDirectory, xUnitFullUrl, "xunit-report.xml");
 } catch (IOException ioe) {
  Logger.error(this, "Failed to obtain xUnit report from: %s. Error: %s", xUnitUrl,
    ioe.getMessage());
 }
}

代码示例来源:origin: com.jcabi/jcabi-log

@Override
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public void run() {
  try {
    this.origin.run();
    // @checkstyle IllegalCatch (1 line)
  } catch (final RuntimeException ex) {
    if (this.rethrow) {
      Logger.warn(this, "escalated exception: %s", this.tail(ex));
      throw ex;
    }
    Logger.warn(this, "swallowed exception: %s", this.tail(ex));
    // @checkstyle IllegalCatch (1 line)
  } catch (final Error error) {
    if (this.rethrow) {
      Logger.error(this, "escalated error: %s", this.tail(error));
      throw error;
    }
    Logger.error(this, "swallowed error: %s", this.tail(error));
  }
  if (Thread.currentThread().isInterrupted()) {
    Thread.currentThread().interrupt();
    throw new IllegalStateException(
      "the thread has been interrupted"
    );
  }
}

代码示例来源:origin: com.jcabi/jcabi-aspects

/**
 * Log one line.
 * @param level Level of logging
 * @param log Destination log
 * @param message Message to log
 * @param params Message parameters
 * @checkstyle ParameterNumberCheck (3 lines)
 */
public static void log(final int level, final Object log,
  final String message, final Object... params) {
  if (level == Loggable.TRACE) {
    Logger.trace(log, message, params);
  } else if (level == Loggable.DEBUG) {
    Logger.debug(log, message, params);
  } else if (level == Loggable.INFO) {
    Logger.info(log, message, params);
  } else if (level == Loggable.WARN) {
    Logger.warn(log, message, params);
  } else if (level == Loggable.ERROR) {
    Logger.error(log, message, params);
  }
}

代码示例来源:origin: com.rexsl/rexsl

@Override
public void log(final String str) {
  Logger.info(this, str);
}

相关文章