Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aggregated logger for event and stat #229

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
*~
.DS_Store
/bazel-*
.ijwb

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ be `runtime` scope.
### 2. Add an import for [`FluentLogger`]

```java
import com.google.common.flogger.FluentLogger;

```

### 3. Create a `private static final` instance
Expand Down
11 changes: 11 additions & 0 deletions api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ java_binary(
deps = ["@google_bazel_common//third_party/java/asm"],
)

java_binary(
name = "fluent_aggregated_logger_example",
srcs = ["src/main/java/com/google/common/flogger/example/FluentAggregatedLoggerExample.java"],
main_class = "com.google.common.flogger.example.FluentAggregatedLoggerExample",
deps = [
":api",
":system_backend",
"@google_bazel_common//third_party/java/log4j",
],
)

genrule(
name = "gen_platform_provider",
outs = ["platform_provider.jar"],
Expand Down
72 changes: 4 additions & 68 deletions api/src/main/java/com/google/common/flogger/AbstractLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package com.google.common.flogger;

import static com.google.common.flogger.util.Checks.checkNotNull;

import com.google.common.flogger.backend.LogData;
import com.google.common.flogger.backend.LoggerBackend;
import com.google.common.flogger.backend.LoggingException;
import com.google.errorprone.annotations.CheckReturnValue;

import java.util.logging.Level;

import static com.google.common.flogger.util.Checks.checkNotNull;

/**
* Base class for the fluent logger API. This class is a factory for instances of a fluent logging
* API, used to build log statements via method chaining.
*
* @param <API> the logging API provided by this logger.
*/
@CheckReturnValue
public abstract class AbstractLogger<API extends LoggingApi<API>> {
public abstract class AbstractLogger {
private final LoggerBackend backend;

/**
Expand All @@ -43,61 +42,6 @@ protected AbstractLogger(LoggerBackend backend) {
this.backend = checkNotNull(backend, "backend");
}

// ---- PUBLIC API ----

/**
* Returns a fluent logging API appropriate for the specified log level.
* <p>
* If a logger implementation determines that logging is definitely disabled at this point then
* this method is expected to return a "no-op" implementation of that logging API, which will
* result in all further calls made for the log statement to being silently ignored.
* <p>
* A simple implementation of this method in a concrete subclass might look like:
* <pre>{@code
* boolean isLoggable = isLoggable(level);
* boolean isForced = Platform.shouldForceLogging(getName(), level, isLoggable);
* return (isLoggable | isForced) ? new SubContext(level, isForced) : NO_OP;
* }</pre>
* where {@code NO_OP} is a singleton, no-op instance of the logging API whose methods do nothing
* and just {@code return noOp()}.
*/
public abstract API at(Level level);

/** A convenience method for at({@link Level#SEVERE}). */
public final API atSevere() {
return at(Level.SEVERE);
}

/** A convenience method for at({@link Level#WARNING}). */
public final API atWarning() {
return at(Level.WARNING);
}

/** A convenience method for at({@link Level#INFO}). */
public final API atInfo() {
return at(Level.INFO);
}

/** A convenience method for at({@link Level#CONFIG}). */
public final API atConfig() {
return at(Level.CONFIG);
}

/** A convenience method for at({@link Level#FINE}). */
public final API atFine() {
return at(Level.FINE);
}

/** A convenience method for at({@link Level#FINER}). */
public final API atFiner() {
return at(Level.FINER);
}

/** A convenience method for at({@link Level#FINEST}). */
public final API atFinest() {
return at(Level.FINEST);
}

// ---- HELPER METHODS (useful during sub-class initialization) ----

/**
Expand All @@ -115,14 +59,6 @@ protected String getName() {
return backend.getLoggerName();
}

/**
* Returns whether the given level is enabled for this logger. Users wishing to guard code with a
* check for "loggability" should use {@code logger.atLevel().isEnabled()} instead.
*/
protected final boolean isLoggable(Level level) {
return backend.isLoggable(level);
}

// ---- IMPLEMENTATION DETAIL (only visible to the base logging context) ----

/**
Expand Down
103 changes: 103 additions & 0 deletions api/src/main/java/com/google/common/flogger/AbstractMessageLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2012 The Flogger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.flogger;

import static com.google.common.flogger.util.Checks.checkNotNull;

import com.google.common.flogger.backend.LogData;
import com.google.common.flogger.backend.LoggerBackend;
import com.google.common.flogger.backend.LoggingException;
import com.google.errorprone.annotations.CheckReturnValue;
import java.util.logging.Level;

/**
* Base class for the fluent logger API. This class is a factory for instances of a fluent logging
* API, used to build log statements via method chaining.
*
* @param <API> the logging API provided by this logger.
*/
@CheckReturnValue
public abstract class AbstractMessageLogger<API extends LoggingApi<API>> extends AbstractLogger{

/**
* Constructs a new logger for the specified backend.
*
* @param backend the logger backend which ultimately writes the log statements out.
*/
protected AbstractMessageLogger(LoggerBackend backend) {
super(backend);
}

// ---- PUBLIC API ----

/**
* Returns a fluent logging API appropriate for the specified log level.
* <p>
* If a logger implementation determines that logging is definitely disabled at this point then
* this method is expected to return a "no-op" implementation of that logging API, which will
* result in all further calls made for the log statement to being silently ignored.
* <p>
* A simple implementation of this method in a concrete subclass might look like:
* <pre>{@code
* boolean isLoggable = isLoggable(level);
* boolean isForced = Platform.shouldForceLogging(getName(), level, isLoggable);
* return (isLoggable | isForced) ? new SubContext(level, isForced) : NO_OP;
* }</pre>
* where {@code NO_OP} is a singleton, no-op instance of the logging API whose methods do nothing
* and just {@code return noOp()}.
*/
public abstract API at(Level level);

/** A convenience method for at({@link Level#SEVERE}). */
public final API atSevere() {
return at(Level.SEVERE);
}

/** A convenience method for at({@link Level#WARNING}). */
public final API atWarning() {
return at(Level.WARNING);
}

/** A convenience method for at({@link Level#INFO}). */
public final API atInfo() {
return at(Level.INFO);
}

/** A convenience method for at({@link Level#CONFIG}). */
public final API atConfig() {
return at(Level.CONFIG);
}

/** A convenience method for at({@link Level#FINE}). */
public final API atFine() {
return at(Level.FINE);
}

/** A convenience method for at({@link Level#FINER}). */
public final API atFiner() {
return at(Level.FINER);
}

/** A convenience method for at({@link Level#FINEST}). */
public final API atFinest() {
return at(Level.FINEST);
}

protected final boolean isLoggable(Level level) {
return getBackend().isLoggable(level);
}
}
Loading