Skip to content

Commit 3bd338c

Browse files
committed
don't enable logstruct JSON logging by default for rake or rails console
1 parent fc286c7 commit 3bd338c

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ We support all your other favorite gems too, like Sidekiq, Sentry, and Shrine. (
66

77
## Features
88

9-
- JSON logging enabled by default in production and test environments
9+
- JSON logging enabled by default for server processes in production and test environments (automatically disabled for console and Rake tasks)
1010
- ActionMailer integration for email delivery logging
1111
- ActiveJob integration for job execution logging
1212
- Sidekiq integration for background job logging
@@ -58,11 +58,13 @@ Once initialized (and enabled), the gem automatically includes its modules into
5858
- Rails `config.filter_parameters` are merged into LogStruct's filters and then cleared (to avoid double filtering). Configure sensitive keys via `LogStruct.config.filters`.
5959
- When `RAILS_LOG_TO_STDOUT` is set, we log to STDOUT only. Otherwise, we log to STDOUT by default without adding a file appender to avoid duplicate logs.
6060

61-
### Development behavior
61+
### Default behavior by process type
6262

63-
- Disabled by default in development. Enable explicitly via `LOGSTRUCT_ENABLED=true` or `LogStruct.configure { |c| c.enabled = true }`.
64-
- When enabled in development, LogStruct now defaults to production‑style JSON output so you can preview exactly what will be shipped in prod.
65-
- You can opt back into the colorful human formatter with:
63+
- **Server processes** (`rails server`): JSON logging is enabled by default in production and test environments
64+
- **Console and Rake tasks** (`rails console`, `rake`, etc.): JSON logging is disabled by default in all environments, providing human-readable logs instead
65+
- **Development environment**: Disabled by default for all process types. Enable explicitly via `LOGSTRUCT_ENABLED=true` or `LogStruct.configure { |c| c.enabled = true }`.
66+
67+
When enabled in development, LogStruct defaults to production‑style JSON output so you can preview exactly what will be shipped in prod. You can opt back into the colorful human formatter with:
6668

6769
```ruby
6870
LogStruct.configure do |c|
@@ -71,6 +73,8 @@ LogStruct.configure do |c|
7173
end
7274
```
7375

76+
To force JSON logs in console or Rake tasks (e.g., for debugging), set `LOGSTRUCT_ENABLED=true` in your environment.
77+
7478
## Documentation
7579

7680
Please see the [documentation](https://logstruct.com/docs) for more details. (All code examples are type-checked and tested, and it's harder to keep a README up to date.)

docs/app/docs/configuration/page.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export default function ConfigurationPage() {
5858
</strong>{' '}
5959
is undefined, LogStruct will be <strong>enabled</strong> if the
6060
current Rails environment is listed in{' '}
61-
<code>config.enabled_environments</code>.
61+
<code>config.enabled_environments</code> <strong>AND</strong> the
62+
process is a server process (not console or Rake tasks). This ensures
63+
humans using <code>rails console</code> or <code>rake</code> tasks get
64+
human-readable logs, while server processes get JSON logs.
6265
</li>
6366
<li>
6467
Finally, you can manually set <code>config.enabled</code> in an
@@ -118,6 +121,14 @@ end`}
118121
<code>config.enabled_environments</code>.
119122
</Callout>
120123

124+
<Callout type="info">
125+
To force JSON logs in console or Rake tasks (e.g., for debugging or
126+
inspecting the exact JSON output), set{' '}
127+
<code>LOGSTRUCT_ENABLED=true</code> when running the command:
128+
<br />
129+
<code>LOGSTRUCT_ENABLED=true rails console</code>
130+
</Callout>
131+
121132
<HeadingWithAnchor id="integration-configuration">
122133
Integration Configuration
123134
</HeadingWithAnchor>

docs/app/docs/getting-started/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function GettingStartedPage() {
2929
<CodeBlock language="bash">bundle install</CodeBlock>
3030

3131
<p className="text-neutral-600 dark:text-neutral-400 mt-6">
32-
{`LogStruct is now installed and will automatically enable JSON structured logging in the test and production environments.`}
32+
{`LogStruct is now installed and will automatically enable JSON structured logging for server processes in the test and production environments. Console and Rake tasks will use human-readable logs by default.`}
3333
</p>
3434

3535
<HeadingWithAnchor id="basic-configuration" level={2}>

docs/app/docs/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export default function DocsPage() {
2222
</HeadingWithAnchor>
2323
<ul className="list-disc list-inside space-y-2 text-neutral-600 dark:text-neutral-400">
2424
<li>
25-
JSON logging enabled by default in production and test environments.
25+
JSON logging enabled by default for server processes in production and
26+
test environments (automatically disabled for console and Rake tasks).
2627
</li>
2728
<li>
2829
Sets up <a href="https://github.com/roidrage/lograge">Lograge</a> for

lib/log_struct/concerns/configuration.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ def set_enabled_from_rails_env!
4545
# - Sets enabled=true only when value is "true", "yes", "1", etc.
4646
# - Sets enabled=false when value is any other value
4747
# 2. Otherwise, check if current Rails environment is in enabled_environments
48+
# AND Rails::Server is defined (to exclude console and Rake tasks)
4849
# 3. Otherwise, leave as config.enabled (defaults to true)
4950

5051
# Then check if LOGSTRUCT_ENABLED env var is set
5152
config.enabled = if ENV["LOGSTRUCT_ENABLED"]
5253
# Override to true only if env var is "true"
5354
%w[true t yes y 1].include?(ENV["LOGSTRUCT_ENABLED"]&.strip&.downcase)
5455
else
55-
config.enabled_environments.include?(::Rails.env.to_sym)
56+
# Only enable for server processes, not console or Rake tasks
57+
config.enabled_environments.include?(::Rails.env.to_sym) && !!defined?(::Rails::Server)
5658
end
5759
end
5860

0 commit comments

Comments
 (0)