Logger
@adminjs/logger
AdminJS has some extra plugins which extend its basic functionality. One of them is logger.
The logger's purpose is to keep track of selected resources (i.e. tables) Every action performed on resource (new, edit, delete or bulkDelete) is registered in special object and persisted in database. Information about changes are monitored, so we can inspect and compare changes in every field of a table.
Installation is pretty simple. First we have to install @adminjs/logger package
$ yarn add @adminjs/loggerWe can use any database and any ORM package as described in Adapters section.
Then we have to define Log entity - the place we will track changes. Below you can find example
import { DataTypes, Model } from 'sequelize';
import db from './sequelize.connection.js';
import User from './user.entity.js';
export interface ILog = {
id: number;
action: string;
resource: string;
userId: number;
recordId: number;
recordTitle: string;
difference: string;
createdAt: Date;
updatedAt: Date;
};
export class Log extends Model<ILog> {
id: number;
createdAt: Date;
updatedAt?: Date;
recordId: number;
recordTitle: string | null;
difference: Record<string, unknown> | null;
action: string;
resource: string;
userId: number;
}
Log.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
action: {
type: new DataTypes.STRING(128),
allowNull: false,
},
resource: {
type: new DataTypes.STRING(128),
allowNull: false,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
recordId: {
type: DataTypes.INTEGER,
allowNull: false,
},
recordTitle: {
type: new DataTypes.STRING(128),
allowNull: false,
},
difference: {
type: DataTypes.JSONB,
allowNull: true,
},
},
{
sequelize: db,
tableName: 'logs',
timestamps: true,
}
);
export default Log;EntityLog is related to entity User because userId holds reference to user who made changes
There is one thing worth to mention. We have to enable authorization before using this feature, because we will need user's ID for corresponding changes.
To get logger to work, add extra property to resource config. Below you can find simple example of such configuration.
To have Log resource appear in AdminJS panel, we have to define it first.
@adminjs/logger exports createLoggerResource function which does most of the work for you. You can customize it using it's configuration argument.
Last updated