Abseil Logging

By Andy Getzendanner, Abseil Engineer

We are pleased to announce, at long last, the initial availability of the Abseil Logging library. This library provides facilities for writing short text messages about the status of a program to stderr, disk files, or other sinks (via an extension API).

The core interface is the LOG macro, which has a streaming interface like std::ostream’s:

absl::StatusOr<absl::Duration> GetUserAge(absl::string_view user_name) {
  int user_id = GetUserId(user_name);
  if (user_id == -1) {
    LOG(ERROR) << "No user found named " << user_name;
    return absl::NotFoundError(absl::StrCat("No user found named ", user_name));
  }
  return age_map[user_id];
}

The library also supports terminating the process via a severity level named FATAL:

absl::Duration GetUserAge(absl::string_view user_name) {
  int user_id = GetUser(user_name);
  if (user_id == -1) {
    LOG(FATAL) << "No user named " << user_name << " found";
    // No need for a return statement; this line is unreachable.
  }
  return age_map[user_id];
}

The CHECK macro family provides assert()-like precondition checking and process termination in all compilation modes with better error messages:

absl::Duration GetUserAge(absl::string_view user_name) {
  int user_id = GetUser(user_name);
  CHECK(user_id != -1) << "No user named " << user_name << " found";
  return age_map[user_id];
}

If you are familiar with the Google Logging (glog) library, these examples will look very familiar; that project and this one share a common ancestor. The core macro APIs are essentially unchanged, however other interfaces like the extension points have changed quite a bit. While those changes aim to improve interfaces, performance, extensibility, etc., the main reason to choose Abseil Logging over glog is for its compatibility and integration with Abseil and with Google’s internal code now and in the future.

For more information, consult the Abseil Logging library documentation.


Subscribe to the Abseil Blog