From dd6350aa413e766814b0168c2187ac57915a5f47 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 19 Jan 2026 13:52:27 -0600 Subject: [PATCH] Add Check The Current Named Log Level as a Rails TIL --- README.md | 3 +- rails/check-the-current-named-log-level.md | 46 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 rails/check-the-current-named-log-level.md diff --git a/README.md b/README.md index 769b6c4..421fcaa 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1731 TILs and counting..._ +_1732 TILs and counting..._ See some of the other learning resources I work on: @@ -1073,6 +1073,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Check If ActiveRecord Update Fails](rails/check-if-activerecord-update-fails.md) - [Check If Any Records Have A Null Value](rails/check-if-any-records-have-a-null-value.md) - [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md) +- [Check The Current Named Log Level](rails/check-the-current-named-log-level.md) - [Clean Up Memory Hungry Rails Console Processes](rails/clean-up-memory-hungry-rails-console-processes.md) - [Code Statistics For An Application](rails/code-statistics-for-an-application.md) - [Columns With Default Values Are Nil On Create](rails/columns-with-default-values-are-nil-on-create.md) diff --git a/rails/check-the-current-named-log-level.md b/rails/check-the-current-named-log-level.md new file mode 100644 index 0000000..be4cc54 --- /dev/null +++ b/rails/check-the-current-named-log-level.md @@ -0,0 +1,46 @@ +# Check The Current Named Log Level + +I'm connected to a `rails console` session for an active Rails app. I want to +check the current log level. + +```ruby +> Rails.logger.level +=> 1 +``` + +The `1` doesn't mean much to me at a glance. I can translate that to the +severity level using the `Logger::SEV_LABLE` constant. + +```ruby +[44] pry(main)> Logger::SEV_LABEL[Rails.logger.level] +=> "INFO" +``` + +Ah yes, `INFO`, that makes sense as the default. + +I can see all the severity levels by inspecting the constant itself. + +```ruby +[45] pry(main)> Logger::SEV_LABEL +=> ["DEBUG", "INFO", "WARN", "ERROR", "FATAL", "ANY"] +``` + +As I convenience, I can set the label using the index, the string, or even a +symbol. + +```ruby +> Rails.logger.level +=> 1 +> Rails.logger.level = "WARN" +=> "WARN" +> Rails.logger.level +=> 2 +> Rails.logger.level = :debug +=> :debug +> Rails.logger.level +=> 0 +``` + +See the [Debugging Rails Applications +guide](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels) +for more details.