- Published on
How to disable logger in Flutter tests
- Authors
- Name
- Emanoel Oliveira
Introduction
Logs are an important part of any type of application, and mobile applications are no different.
From them you can track your application flows, understand specifically the path a user has taken in your application, as well as identify errors that are occurring.
One of the most widely used libraries in Flutter
to generate logs
is logger
and this article will show how to disable it when running tests.
Logs in tests
Logs
are extremely useful for code in development and production environments, but they are extremely unnecessary in the context of testing.
Thinking about performance in a unit test suite, it is completely unnecessary to present logs
when executing automated tests, because the intention is only to run the tests and in some cases generate the coverage
, in which case presenting logs
will only increase the test execution time.
logger
The logger
library is very simple to implement. Generally, with a few lines of code we have a logs client
ready to be used:
import 'package:logger/logger.dart';
final Logger logger = Logger(
printer: PrettyPrinter(),
output: null,
);
However, with this implementation you will notice that when you run the tests, the logs
will accumulate and leave your suite dirty and with a lot of information.
At this point, some questions arise:
So how can we stop presenting these
logs
?Do we have to implement something in the test layer to stop them appearing?
Do we have to
mock
the client to stop it displaying thelogs
?
The answer is no, you don't need any kind of code in the test layer to disable logs
in tests.
filter
property
The Well, it is actually possible to mock
the client
and prevent it from displaying the log
, but in this case it would be unnecessary because the logger
library has the filter
property, which is specified in its construction.
With the filter
property you can define a rule for displaying the log
. This turns out to be a more effective solution than mocking
the client
, since the configuration is centralized in the logger
build, which means that we only have to configure it in one place and from then on, the logs
won't be displayed in any tests.
Well, the filter
property gets a class that extends from LogFilter
, so in our example, we'll call our filter MyFilter
:
import 'dart:io';
import 'package:logger/logger.dart';
class MyFilter extends LogFilter {
bool shouldLog(LogEvent event) {
final isTest = Platform.environment.containsKey('FLUTTER_TEST');
return !isTest;
}
}
final Logger logger = Logger(
filter: MyFilter(),
printer: PrettyPrinter(),
output: null,
);
So, to disable the logs
that would be displayed when running the tests, you just need to use a native code of flutter that lets you know if the code in question is running in a test environment in the shouldLog
function:
Platform.environment.containsKey('FLUTTER_TEST')
With this simple code it is possible to know that the code is running in a test environment and, based on this value, to disable logs
in tests simply deny their return, since we only want to enable logs
when the environment is not a test environment.
Conclusion
With this simple filter applied to the logger
your test suite will be cleaner, without unnecessary and irrelevant information.