Comment on page
Logger
A simple console log plugin to log actions as they are processed.
npm install @ngxs/logger-plugin --save
# or if you are using yarn
yarn add @ngxs/logger-plugin
Add the
NgxsLoggerPluginModule
plugin to your root app module:import { NgxsModule } from '@ngxs/store';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
@NgModule({
imports: [NgxsModule.forRoot([]), NgxsLoggerPluginModule.forRoot()]
})
export class AppModule {}
The plugin supports the following options passed via the
forRoot
method:logger
: Supply a different logger, useful for logging to backend. Defaults toconsole
.collapsed
: Collapse the log by default or not. Defaults totrue
.disabled
: Disable the logger during production. Defaults tofalse
.filter
: Filter actions to be logged. Takes action and state snapshot as parameters. Default predicate returnstrue
for all actions.
import { NgxsModule, getActionTypeFromInstance } from '@ngxs/store';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
import { environment } from '../environments/environment';
import { customLogger } from './path/to/custom/logger';
import { SomeAction } from './path/to/some/action';
@NgModule({
imports: [
NgxsModule.forRoot([]),
NgxsLoggerPluginModule.forRoot({
// Use customLogger instead of console
logger: customLogger,
// Do not collapse log groups
collapsed: false,
// Do not log in production mode
disabled: environment.production,
// Do not log SomeAction
filter: action => getActionTypeFromInstance(action) !== SomeAction.type
})
]
})
export class AppModule {}
Thefilter
predicate takes state snapshot as the second parameter. This should prove useful for some edge cases. However, beware of the fact that the predicate is called for every action dispatched. You may consider using a memoized function for filters more complicated than a simple action comparison.
You should always include the logger as the last plugin in your configuration. For instance, if you were to include logger before a plugin like the storage plugin, the initial state would not be reflected.
Last modified 1yr ago