DEV Community

Cover image for Tracking and Security in Payload CMS with the Payload-Auditor Plugin
seyed mojtaba shadab
seyed mojtaba shadab

Posted on

Tracking and Security in Payload CMS with the Payload-Auditor Plugin

Before we get into the main topic of the article, let's first learn about Payload CMS.

A Brief Introduction to Payload CMS

Payload CMS is a modern, fully JavaScript-based, headless content management system. Unlike many traditional CMSs, Payload has a strong focus on customization, high performance, and a developer-centric experience.

Instead of using pre-built panels, it allows you to fully define the structure and behavior of your admin panel through code. Some of its notable features include:

  • Full support for TypeScript

  • Advanced system for defining collections and fields

  • Ability to add hooks and detailed access control

  • Headless design for use in separate Frontend projects (React, Next.js, Vue, etc.)

  • And strong support for authentication, file upload, and REST and GraphQL APIs

Overall, Payload CMS is a powerful option for developers who want full control over the structure and functionality of their CMS β€” without having to deal with the limitations of traditional platforms.

Why do we need logging and tracing in Payload?

In the world of modern software, especially in projects built with headless CMSs like Payload CMS, the issue of security, transparency, and user behavior analysis is of utmost importance.

As the scale of the project grows and the number of users or administrators increases, knowing who did what, when, and where is no longer an optional feature β€” it is a vital need.

Here are some reasons why logging and tracing is necessary:

  • πŸ§‘β€πŸ’» Track user actions: Who changed the information? When was a file deleted? Did someone log in with the wrong access?
  • πŸ›‘οΈ Increase security: Recording suspicious or sensitive events (such as changes to user information or suspicious logins) is the first line of defense against attacks.
  • 🧾 Organizational Transparency: In multi-person teams, knowing the history of actions prevents repetitive errors and creates order in team processes.
  • πŸ§ͺ Debugging and analyzing system behavior: Having a history of operations is very helpful when developing or investigating problems.

Payload Auditor Plugin: Answering Your Monitoring and Security Needs

This is where the Payload Auditor plugin comes in. It is designed to work seamlessly with Payload CMS, allowing for detailed logging of operations, event tracking, and greater control over backend security.

Not only does the plugin allow you to specify which collections to track, but you can even fine-tune the type of operation (create, update, delete) per hook. Simply put, you have complete control over what gets logged β€” without any added overhead or unnecessary complexity.

I’ll give you a more complete explanation later.


Why Payload Auditor?

Common Problems in Payload Projects Without a Logging System

In many projects built with Payload CMS, developers focus primarily on data structure, admin panel design, and API interaction β€” but an important part that is often overlooked is the logging and event monitoring system.

Here are some common problems that arise in the absence of a proper logging system:


πŸ” Lack of Clear Change View
In projects with multiple content managers or developers, it is often unclear who made what changes and when. This can lead to confusion, repeated errors, or even damage to important data.


πŸ› οΈ Difficult Debugging
In the event of a problem, the lack of detailed reporting of the operations performed causes the development team to spend a lot of time searching through data or code to understand what happened.


πŸ›‘οΈ High Security Risks
When no traces of sensitive operations (such as updating user information or deleting data) remain, it becomes very difficult to identify suspicious actions or possible abuses.


πŸ“‰ Lack of internal documentation of system performance
In organizational or team projects, automated documentation of user and system behavior plays a vital role in analyzing and improving processes. Otherwise, this task is either completely forgotten or performed manually and incompletely.


⏸️ Lack of tracking and targeted stopping of operations
Without a monitoring system, it is not possible to centrally specify whether logging should be stopped in a specific section or only certain operations (such as creation or deletion) should be logged.


Payload Auditor fills these gaps β€” as a simple, flexible, and powerful tool for:

  • Tracking all important activities
  • Accurately recording the history of operations
  • Controlling access to logs
  • and increasing the overall security of your project

Benefits of Using the Payload Auditor Plugin

Using the Payload Auditor plugin allows developers to make their projects more professional, secure, and more analyzable. Here are the most important benefits of this tool:


βœ… Full Activity Tracking
This plugin allows for accurate recording of operations such as user creation, editing, deletion, and login. You can know who did what, when, and what β€” something that is practically impossible in systems without logging.


πŸ” More security in multi-user environments
In projects where multiple users with different access levels are working, tracking and limiting access to logs plays an important role in preventing abuse or unwanted changes. This plugin allows you to control access to reports and closely monitor activities.


🏒 Suitable for SaaS and Enterprise Projects
In SaaS and Enterprise-level projects, detailed reports, change history, and analytical tools are a must. By providing these features, Payload Auditor helps you build a professional, reliable, and industry-standard product.


βš™οΈ High customization for specific needs
Through flexible settings, this plugin allows you to track only the collections that are important to you, select the type of operations, and even disable its functionality in certain situations.


πŸ“Š Analysis of behavioral and operational data
By recording structured events, you can perform valuable analyses on user behavior, system problems, or usage patterns in the future. This information can be useful in optimizing the user experience or making management decisions.

Install and set up the Payload Auditor plugin

Setting up the payload-auditor plugin is very simple and quick, and it can be added to the Payload CMS project in a few short steps. Here are the basic installation and configuration steps:


🧱 Installing with npm / yarn / pnpm

First, add the plugin to your project using your preferred package manager (although we recommend using pnpm):

npm install payload-auditor
Enter fullscreen mode Exit fullscreen mode

Or if you are using yarn:

yarn add payload-auditor
Enter fullscreen mode Exit fullscreen mode

Or with pnpm:

pnpm add payload-auditor
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Adding the plugin to the Payload configuration

After installation, simply import and enable the plugin in the Payload configuration file (usually payload.config.ts or payload.config.js).

import { buildConfig } from 'payload/config'
import { auditorPlugin } from 'payload-auditor'

export default buildConfig({
  collections: [
    // ... your collections here
  ],
  plugins: [
    auditorPlugin({
      // plugin settings go here
    }),
  ],
})
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ Basic configuration (simple example)

In this simple example, we will configure only one collection called media for logging. Payload-Auditor uses payload cms hooks for logging operations. In this configuration, only update operations will be logged using the afterChange hook:

auditorPlugin({
  collection: {
    trackCollections: [
      {
        slug: 'media',
        hooks: {
          afterChange: {
            update: {
              enabled: true,
            },
          },
        },
      },
    ],
  },
})
Enter fullscreen mode Exit fullscreen mode

With these simple settings, the plugin starts logging changes to the media collection.

Payload Auditor Plugin Configuration Options

The payload-auditor plugin is designed for high flexibility, allowing you to specify exactly which collections are logged, under what conditions, and with what operations. Here are the most important configuration options:

πŸ•’ automation

This option manages automated operations in the logging system. For example, you can specify how many old logs you want to delete. You can even specify which logs are considered old:

      automation: {
        logCleanup: {
          strategy: {
            name: 'manual',
            amount: 66,
            olderThan: '2m',
          },
        },
      },
Enter fullscreen mode Exit fullscreen mode

In the above code, logs that are more than two minutes old are considered old and 66 of them are deleted each time. Please note that this is just an example.

πŸ“ trackCollections

In this option, you introduce a list of collections whose activities you want to track. For each collection you can have its own settings. The settings in this section are very flexible, meaning you can enable all Payload hooks for each collection and even specify which operations you want to enable.

trackCollections: [
  {
    slug: 'users',
    hooks: {
      afterChange: {
        update: { enabled: true },
        create: { enabled: true },
      },
    },
  },
]
Enter fullscreen mode Exit fullscreen mode

🎯 Filtering operations inside hooks

The plugin allows you to log only some specific operations (for example, only update or delete).
This way, the logs become more targeted and lightweight.

hooks: {
  afterOperation: {
    delete: { enabled: true },
    create: { enabled: false },
  },
}
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Complete example of an advanced configuration

In the following example, two collections (users and posts) are tracked. In the users collection, only the update operation is logged in the afterChange hook. In the posts collection, only the delete is logged in the afterOperation hook. Also, the logs are deleted after 30 days:

auditorPlugin({
      automation: {
        logCleanup: {
          strategy: {
            name: 'manual',
            amount: 150,
            olderThan: '7d',
          },
        },
      },
      collection: {
        trackCollections: [
          {
            slug: 'users',
            hooks: {
              afterChange: {
                update: {
                  enabled: true,
                },
              },
            },
          },
          {
            slug: 'posts',
            hooks: {
              afterOperation: {
                delete: {
                  enabled: true,
                },
              },
            },
          },
        ],
      },
    })
Enter fullscreen mode Exit fullscreen mode

How the Payload Auditor Plugin Works

The payload-auditor plugin uses the very powerful hooks feature of Payload CMS to log and track important operations at the collection level. This approach makes logging non-destructive, modular, and controllable.


πŸ”„ Using hooks for logging

By default, Payload CMS provides a set of hooks for each collection, including:

  • beforeChange
  • afterChange
  • beforeOperation
  • afterOperation
  • and all payload cms hooks...

By connecting to these hooks, the payload-auditor plugin intercepts and logs all operations that occur in your project β€” but only if you have enabled it.

πŸ“Œ Unlike some other tools, this plugin only logs when you tell it to, so there is no additional overhead to your project.


βš™οΈ Differences between operations (create, update, delete)

For each hook you can specify exactly which type of operation should be logged. For example:

hooks: {
  beforeValidate: {
    update: { enabled: true },
    create: { enabled: false },
  },
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  • The update operation is logged in the afterChange hook.
  • But the create operation is ignored. Of course, it is also ignored if you do not fully define this operation. This feature is mostly for when you want to ignore this operation for now and enable it later.

This level of fine-grained control allows you to tailor the logging system to exactly the needs of your project.


⏸️ Ability to temporarily stop logging for a specific collection

You may want to temporarily stop logging on a collection β€” for example, to fix a bug or during development. The plugin provides this feature as well. Just set the enabled property value to false or remove the collection from the trackCollections list.

Example:

{
  slug: 'comments',
  enabled: false, // This collection is temporarily untracked.
}
Enter fullscreen mode Exit fullscreen mode

Or remove it altogether so that no hooks are attached to it.

This design is both simple and extensible


Usage Scenarios

The payload-auditor plugin is not just a logging tool; it is a tool for analysis, security, and better management of real projects. Here are some of the most common scenarios where this plugin will be very useful:

πŸ” Logging user logins and profile changes

In projects where users can log into the admin panel, accurate tracking of when and how they log in or changes to their profile is very important from a security and support perspective.

By enabling logging on the users collection (assuming that site users are stored in a collection with the slug users), you can:

  • Identify who logged in and when.
  • Document changes to email, name, password, or user role.
  • Track support reports faster.

πŸ” Security Issues Investigation and User Behavior Analysis

In many cases, suspicious behavior or security issues can be identified simply by looking at the logs. This plugin allows you to:

  • Track unusual deletion or editing operations.
  • Analyze specific user or admin behaviors and identify dangerous patterns.
  • Generate a detailed report for security or compliance audits (in development).

πŸ‘₯ Use in multi-user projects with different access levels

In projects where several people with different roles work on a CMS, such as:

  • Content Editors
  • Support Agents
  • Admins

Managing changes and answering the question "Who did what?" becomes extremely difficult.
The payload-auditor plugin is essential in such environments:

  • Accurately logging operations for each role
  • Preventing unwanted or erroneous changes
  • Helping to build team discipline and trust in large teams

Best practices for using Payload Auditor

To use the payload-auditor plugin effectively and securely, it is recommended to follow some tips and recommendations. Here are some important ones:


βœ… 1. Optimal and targeted configuration

Not all collections need logging. Only select those that contain sensitive data or important operations (such as users, orders, payments).

Recommendation:

  • Only enable hooks and operations that really need to be tracked.
  • This will reduce the size of the logs and improve system performance.

🧹 2. Intelligent long-term log retention

If not managed properly, logs can cause database size to increase and performance to decrease over time. Use the automation option to control the lifetime of logs.

Example:

 automation: {
        //  ...
      },
Enter fullscreen mode Exit fullscreen mode

In sensitive projects:
If you need long-term retention, move logs to archive systems or centralized logging tools (like ELK Stack or Logstash). For this, you can use customLogger which is provided at 4 levels: global, hook level, specific hook level and operation level.


πŸ”’ 3. Restrict access to logs

Not all users should have access to logs. For added security:

  • Only root admins or users with specific roles (e.g. Auditor) should be able to view or analyze logs.
  • Restrict access to log collections using Access Control in Payload.

Simple example:

Accessibility:{
          // Your configuration
        },
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ 4. Testing and Monitoring in the Development Environment

Before using in the production environment, test the plugin’s performance thoroughly in the development or staging environment. Check:

  • Are logs recorded accurately?
  • Are unnecessary operations not logged?
  • Is system performance not degraded?

Following these practices will make the Payload Auditor plugin one of the pillars of your project’s security and professional maintenance, rather than an additional tool.

Conclusion

The Payload Auditor plugin is a powerful and flexible tool that allows you to accurately and securely track user activities and important changes in your Payload CMS projects. With this plugin, you can:

  • Increase project security in multi-user environments,
  • Quickly identify problems and suspicious behavior,
  • Generate detailed and analytical reports for better project management,
  • And experience all these features with high customization capabilities and precise log management.

If you are also looking to improve security and transparency in your Payload CMS projects, we recommend that you definitely try this plugin.


Contribute to development

This project is open source and interested developers can help improve and expand it by providing feedback, suggestions, or contributing to its development. To view the source code, complete documentation, and start contributing, visit its official GitHub repository:

Payload Auditor on GitHub

With this tool, take an important step towards increasing the security, control, and monitoring of your Payload CMS projects and experience more sustainable development.

Resources and Links

To learn more and better use the Payload Auditor plugin, as well as learn related concepts, you can refer to the following resources:


If you’re looking to increase security, transparency, and analytics in your Payload CMS projects, try the Payload Auditor plugin today.
We’re eager to hear your feedback, suggestions, and contributions to the development of this tool.
Check out the project on GitHub, give it a ⭐ star, and join us in building a stronger and more secure Payload ecosystem!

Top comments (0)