adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

[Bug]: adminjs/logger not working

Open SleeveShirtholes opened this issue 2 years ago • 2 comments

Contact Details

Slack

What happened?

Trying to get adminjs/logger setup and can't seem to get it to work. I have gotten to a point where I think I am close but still no luck. Here is my setup:

Model

@Entity({ name: "logs" })
export class Log extends BaseEntity implements ILog {
    @PrimaryGeneratedColumn()
    public id: number;

    @CreateDateColumn({ name: "created_at" })
    public createdAt: Date;

    @UpdateDateColumn({ name: "updated_at" })
    public updatedAt?: Date;

    @Column({ name: "record_id", type: "integer", nullable: false })
    public recordId: number;

    @Column({ name: "record_title", type: "text", nullable: true, default: "" })
    public recordTitle: string | null;

    @Column({ name: "difference", type: "jsonb", nullable: true, default: {} })
    public difference: Record<string, unknown> | null;

    @Column({ name: "action", type: "varchar", length: 128, nullable: false })
    public action: string;

    @Column({ name: "resource", type: "varchar", length: 128, nullable: false })
    public resource: string;

    @Column({ name: "user_id", type: "varchar", nullable: false })
    public userId: string;

    @ManyToOne(() => User)
    @JoinColumn()
    user: User;
}

AdminJS

        // Setting up the adminJs app
        const admin = new AdminJS({
            // databases: [AppDataSource],
            rootPath: "/admin",
            resources: [
                User,
                JobTitle,
                Department,
                UserHistory,
                UserHierarchy,
                ProxyPermission,
                Permission,
                AccessToken,
                createLoggerResource({
                    resource: Log,
                    featureOptions: {
                        propertiesMapping: {
                            recordTitle: "recordTitle", //field to store logged record's title
                        },
                        userIdAttribute: "userId",
                    },
                }),
            ],
        });

The error that I am getting is really not an error but its output in the console:

[AdminJS]: There is no property of the name: "user". Check out the "listProperties" in the resource: "Log"

I've tried both methods mentioned in the docs and neither is getting it to work. I'm not sure if I am missing something and that is causing it not to work. Please provide some guidance. Thank you for your help

Bug prevalence

When visiting the admin panel and deleting, adding, editing any entry in any table

AdminJS dependencies version

"@adminjs/express": "^5.1.0",
"@adminjs/logger": "^4.0.1",
"@adminjs/typeorm": "^4.0.0",
"adminjs": "^6.8.7",

What browsers do you see the problem on?

Chrome

Relevant log output

No response

Relevant code that's giving you issues

No response

SleeveShirtholes avatar Apr 18 '23 21:04 SleeveShirtholes

You're missing user mapping in propertiesMapping

Here's an example of featureOptions that we used in one of our projects.

{
  propertiesMapping: {
    user: 'userId',
    action: 'actionName',
    resource: 'resourceId',
  },
  resourceOptions: {
    resourceId: 'Log',
    navigation: logsNavigation,
    actions: {
      list: {
        isAccessible: or(forAdmins, forSuperAdmins),
      },
      show: {
        isAccessible: or(forAdmins, forSuperAdmins),
      },
    },
  },
  userIdAttribute: 'id',
}

dziraf avatar Apr 19 '23 09:04 dziraf

@dziraf, great that resolved the output that I was getting but it still isn't logging anything. Is there something else that I am missing that needs to be added? Here is my adminJs config:

        const adminJs = new AdminJS({
            databases: [AppDataSource],
            rootPath: "/admin",
            resources: [
                createLoggerResource({
                    resource: Log,
                    featureOptions: {
                        userIdAttribute: "id",
                        propertiesMapping: {
                            user: "userId",
                            resource: "resource",
                            action: "action",
                        },
                        resourceOptions: {
                            navigation: {
                                name: "SectionName",
                                icon: "iconName",
                            },
                        },
                    },
                }),
            ],
        });

Also, does this log anytime a save/delete/edit happens on every entity or does it only log what is done through the adminJS portal?

SleeveShirtholes avatar Apr 19 '23 15:04 SleeveShirtholes