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

mobile: Pass the log level when setting a logger #32131

Merged
merged 4 commits into from
Feb 1, 2024

Conversation

fredyw
Copy link
Member

@fredyw fredyw commented Jan 31, 2024

This PR adds a feature to allow the underlying logging implementation log Envoy logs according to the log levels when setting the logger via setLogger.

For example (for Android):

engineBuilder
  .setLogger { level, msg ->
    if (level == TRACE) {
      Log.v(tag, msg)
    } else if (level == DEBUG) {
      Log.d(tag, msg)
    } else if (level == INFO) {
      Log.i(tag, msg)
    } else if (level == WARN) {
      Log.w(tag, msg)
    } else if (level == ERROR) {
      Log.e(tag, msg)
    } else if (level == CRITICAL) {
      Log.wtf(tag, msg)
    }
  }

Risk Level: low
Testing: unit tests
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: mobile

Copy link

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #32131 was opened by fredyw.

see: more, trace.

@fredyw fredyw force-pushed the log_level branch 18 times, most recently from 04ab5e5 to b8e6a16 Compare February 1, 2024 06:57
Signed-off-by: Fredy Wijaya <fredyw@google.com>
@fredyw
Copy link
Member Author

fredyw commented Feb 1, 2024

/assign @abeyad

Copy link
Contributor

@abeyad abeyad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@@ -30,8 +53,9 @@ LambdaDelegate::~LambdaDelegate() {
logger_.release(logger_.context);
}

void LambdaDelegate::log(absl::string_view msg, const spdlog::details::log_msg&) {
logger_.log(Data::Utility::copyToBridgeData(msg), logger_.context);
void LambdaDelegate::log(absl::string_view msg, const spdlog::details::log_msg& log_msg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know LambdaDelegate is what it's been called from before, but do you know why it's called that? I don't understand what it means. It seems like it has an envoy_logger, which differs it from the DefaultDelegate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is because it takes a function pointer, hence lambda. I agree that it's probably not a very good name. LoggerDelegate (to replace LambdaDelegate) and StderrLoggerDelagate (to replace DefaultDelegate) will probably better names.

@@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
@param networkMonitoringMode Configure how the engines observe network reachability.
*/
- (instancetype)initWithRunningCallback:(nullable void (^)())onEngineRunning
logger:(nullable void (^)(NSString *))logger
logger:(nullable void (^)(int, NSString *))logger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the int is meant to represent the log level? What does the NSString* represent? Should we make this a struct that can also be used in Objective-C? Also, I thought we'd need to use a NSNumber here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NSString* represents the message. I think using a struct is probably overkill given that we only have 2 parameters. Plus, this structure is similar to the C++ function pointer definition. As for using NSNumber, I think using NSInteger is better given that LogLevel is an Int:

public enum LogLevel: Int {

So, I updated the code to use NSInteger instead of int.

logger.log("hello!");
logger.log("goodbye");
logger.log(EnvoyLogger.Level.INFO, "hello!");
logger.log(EnvoyLogger.Level.INFO, "goodbye");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need any tets for non-INFO level (I assume that's the default), e.g. log level set to DEBUG and make sure we get debug but not trace messages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current CronvoyLogger implementation doesn't actually care about the log level and it doesn't even delegate the logging to the underlying logging implementation. It simply logs the messages to disk. I have a plan to fix this in Cronvoy similar to how we can configure the logger with direct EngineBuilder.

The tests that do log + level are in C++. See LambdaDelegateWithLevelTest.

Signed-off-by: Fredy Wijaya <fredyw@google.com>
abeyad
abeyad previously approved these changes Feb 1, 2024
@abeyad abeyad enabled auto-merge (squash) February 1, 2024 16:22
Signed-off-by: Fredy Wijaya <fredyw@google.com>
auto-merge was automatically disabled February 1, 2024 17:02

Head branch was pushed to by a user without write access

@fredyw
Copy link
Member Author

fredyw commented Feb 1, 2024

/retest

1 similar comment
@fredyw
Copy link
Member Author

fredyw commented Feb 1, 2024

/retest

Signed-off-by: Fredy Wijaya <fredyw@google.com>
@abeyad abeyad merged commit 69f3d43 into envoyproxy:main Feb 1, 2024
36 checks passed
@fredyw fredyw deleted the log_level branch February 1, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants