Skip to content

Commit 0021536

Browse files
authored
feat(logger): enhance log level handling (#1476)
* feat: enhance log level handling * chore: changed return type of getLevelName method * chore: changed return type of getLevelName method * docs: add log level docs * docs: add log level docs
1 parent 12284e9 commit 0021536

File tree

7 files changed

+243
-119
lines changed

7 files changed

+243
-119
lines changed

Diff for: docs/core/logger.md

+34-11
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,40 @@ The error will be logged with default key name `error`, but you can also pass yo
388388

389389
## Advanced
390390

391+
### Log levels
392+
393+
The default log level is `INFO` and can be set using the `logLevel` constructor option or by using the `LOG_LEVEL` environment variable.
394+
395+
Logger supports the following log levels:
396+
397+
| Level | Numeric value |
398+
| ---------- | ------------- |
399+
| `DEBUG` | 8 |
400+
| `INFO` | 12 |
401+
| `WARN` | 16 |
402+
| `ERROR` | 20 |
403+
| `CRITICAL` | 24 |
404+
| `SILENT` | 28 |
405+
406+
You can access the current log level by using the `getLevelName()` method. This method returns the name of the current log level as a string. If you want to change the log level at runtime, you can use the `setLogLevel()` method. This method accepts a string value that represents the log level you want to set, both lower and upper case values are supported.
407+
408+
```typescript
409+
--8<-- "docs/snippets/logger/logLevel.ts"
410+
```
411+
412+
If you want to access the numeric value of the current log level, you can use the `level` property. For example, if the current log level is `INFO`, `logger.level` property will return `12`.
413+
414+
#### Silencing logs
415+
416+
The `SILENT` log level provides a simple and efficient way to suppress all log messages without the need to modify your code. When you set this log level, all log messages, regardless of their severity, will be silenced.
417+
418+
This feature is useful when you want to have your code instrumented to produce logs, but due to some requirement or business decision, you prefer to not emit them.
419+
420+
By setting the log level to `SILENT`, which can be done either through the `logLevel` constructor option or by using the `LOG_LEVEL` environment variable, you can easily suppress all logs as needed.
421+
422+
!!! note
423+
Use the `SILENT` log level with care, as it can make it more challenging to monitor and debug your application. Therefore, we advise using this log level judiciously.
424+
391425
### Using multiple Logger instances across your code
392426

393427
The `createChild` method allows you to create a child instance of the Logger, which inherits all of the attributes from its parent. You have the option to override any of the settings and attributes from the parent logger, including [its settings](#utility-settings), any [persistent attributes](#appending-persistent-additional-log-keys-and-values), and [the log formatter](#custom-log-formatter-bring-your-own-formatter). Once a child logger is created, the logger and its parent will act as separate instances of the Logger class, and as such any change to one won't be applied to the other.
@@ -556,17 +590,6 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i
556590
}
557591
```
558592

559-
### Silencing logs
560-
561-
The `SILENT` log level provides a simple and efficient way to suppress all log messages without the need to modify your code. When you set this log level, all log messages, regardless of their severity, will be silenced.
562-
563-
This feature is useful when you want to have your code instrumented to produce logs, but due to some requirement or business decision, you prefer to not emit them.
564-
565-
By setting the log level to `SILENT`, which can be done either through the `logLevel` constructor option or by using the `LOG_LEVEL` environment variable, you can easily suppress all logs as needed.
566-
567-
!!! note
568-
Use the `SILENT` log level with care, as it can make it more challenging to monitor and debug your application. Therefore, we advise using this log level judiciously.
569-
570593
### Custom Log formatter (Bring Your Own Formatter)
571594

572595
You can customize the structure (keys and values) of your log items by passing a custom log formatter, an object that implements the `LogFormatter` abstract class.

Diff for: docs/snippets/logger/logLevel.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
2+
3+
const logger = new Logger();
4+
5+
logger.getLevelName(); // returns "INFO"
6+
logger.setLogLevel('DEBUG');
7+
logger.level; // returns 8

0 commit comments

Comments
 (0)