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/logger

We 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.

import loggerFeature from '@adminjs/logger';

import ResourceModel from './resource.entity.js';
import componentLoader from './component-loader.js';

export default {
  resource: ResourceModel,
  features: [
    loggerFeature({
      componentLoader,
      propertiesMapping: {
        user: 'userId',
      },
      userIdAttribute: 'id',
    }),
  ],
};

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.

import { createLoggerResource } from '@adminjs/logger';

import Log from './logs.entity.js';

const config = {
  resource: Log,
  featureOptions: {
    propertiesMapping: {
      recordTitle: 'title' //field to store logged record's title
    ,
    userIdAttribute: 'id', //primary key currently logged user
    resourceOptions: {
      navigation: {
        name: 'SectionName',
        icon: 'iconName'
      }
    }
  }
}

export default createLoggerResource(config)

Last updated