AuditLogProvider not being triggered?

Hi everyone, I'm trying to automate logging of user activity to mixpanel and stumbled upon AuditLogProvider. Let me know if there's a better solution for this use case by any chance. Anyway, I've tried to create an instance that simply logs to a console and assign it to Refine. It does not seem to be used at all for some reason, even though I don't see tutorials/examples using any extra code. Anyone seems anything wrong with this?
export const auditLogProvider: AuditLogProvider = {
get: async (params) => {
const { resource, meta, action, author, metaData } = params;

return {};
},
create: (params) => {
const { resource, meta, action, author, data, previousData } = params;

console.log("-------create-------");
console.log(resource); // "produts", "posts", etc.
console.log(meta); // { id: "1" }, { id: "2" }, etc.
console.log(action); // "create", "update", "delete"
// author object is `useGetIdentity` hook's return value.
console.log(author); // { id: "1", name: "John Doe" }
console.log(data); // { name: "Product 1", price: 100 }
console.log(previousData); // { name: "Product 1", price: 50 }
console.log("-------/create-------");

return Promise.resolve({ success: true });
},
update: async (params) => {
const { id, name, ...rest } = params;
console.log("-------update-------");
console.log(id); // "1"
console.log(name); // "Created Product 1"
console.log(rest); // { foo: "bar" }
console.log("-------/update-------");

return Promise.resolve({ success: true });
},
};
export const auditLogProvider: AuditLogProvider = {
get: async (params) => {
const { resource, meta, action, author, metaData } = params;

return {};
},
create: (params) => {
const { resource, meta, action, author, data, previousData } = params;

console.log("-------create-------");
console.log(resource); // "produts", "posts", etc.
console.log(meta); // { id: "1" }, { id: "2" }, etc.
console.log(action); // "create", "update", "delete"
// author object is `useGetIdentity` hook's return value.
console.log(author); // { id: "1", name: "John Doe" }
console.log(data); // { name: "Product 1", price: 100 }
console.log(previousData); // { name: "Product 1", price: 50 }
console.log("-------/create-------");

return Promise.resolve({ success: true });
},
update: async (params) => {
const { id, name, ...rest } = params;
console.log("-------update-------");
console.log(id); // "1"
console.log(name); // "Created Product 1"
console.log(rest); // { foo: "bar" }
console.log("-------/update-------");

return Promise.resolve({ success: true });
},
};
and then
<Refine
dataProvider={dataProvider(API_URL)}
authProvider={indiePitcherAuthProvider}
auditLogProvider={auditLogProvider} // here
routerProvider={routerProvider}
notificationProvider={useNotificationProvider}
//....
<Refine
dataProvider={dataProvider(API_URL)}
authProvider={indiePitcherAuthProvider}
auditLogProvider={auditLogProvider} // here
routerProvider={routerProvider}
notificationProvider={useNotificationProvider}
//....
6 Replies
kapa.ai
kapa.ai5w ago
Hi @Petr. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To troubleshoot why your AuditLogProvider is not being triggered, let's ensure that everything is set up correctly. Based on the provided information, your setup seems mostly correct, but there are a few things to double-check: 1. Ensure Correct Import and Usage: Make sure that the AuditLogProvider is correctly imported and used in your App.tsx file. Here is an example setup:
import { Refine } from "@refinedev/core";
import { auditLogProvider } from "./audit-log-provider"; // Ensure this path is correct

export const App = () => {
return <Refine auditLogProvider={auditLogProvider}>{/* ... */}</Refine>;
};

import { Refine } from "@refinedev/core";
import { auditLogProvider } from "./audit-log-provider"; // Ensure this path is correct

export const App = () => {
return <Refine auditLogProvider={auditLogProvider}>{/* ... */}</Refine>;
};

Source (https://refine.dev/docs/guides-concepts/audit-logs/#audit-log-provider) 2. Check the create Method: Your create method looks correct, but ensure that it is being called by performing some CRUD operations in your application. The create method should be triggered on create, update, and delete actions.
export const auditLogProvider: AuditLogProvider = {
get: async (params) => {
const { resource, meta, action, author, metaData } = params;
return {};
},
create: (params) => {
const { resource, meta, action, author, data, previousData } = params;

console.log("-------create-------");
console.log(resource); // "produts", "posts", etc.
console.log(meta); // { id: "1" }, { id: "2" }, etc.
console.log(action); // "create", "update", "delete"
console.log(author); // { id: "1", name: "John Doe" }
console.log(data); // { name: "Product 1", price: 100 }
console.log(previousData); // { name: "Product 1", price: 50 }
console.log("-------/create-------");
return Promise.resolve({ success: true });
},
update: async (params) => {
const { id, name, ...rest } = params;
console.log("-------update-------");
console.log(id); // "1"
console.log(name); // "Created Product 1"
console.log(rest); // { foo: "bar" }
console.log("-------/update-------");

return Promise.resolve({ success: true });
},
};

export const auditLogProvider: AuditLogProvider = {
get: async (params) => {
const { resource, meta, action, author, metaData } = params;
return {};
},
create: (params) => {
const { resource, meta, action, author, data, previousData } = params;

console.log("-------create-------");
console.log(resource); // "produts", "posts", etc.
console.log(meta); // { id: "1" }, { id: "2" }, etc.
console.log(action); // "create", "update", "delete"
console.log(author); // { id: "1", name: "John Doe" }
console.log(data); // { name: "Product 1", price: 100 }
console.log(previousData); // { name: "Product 1", price: 50 }
console.log("-------/create-------");
return Promise.resolve({ success: true });
},
update: async (params) => {
const { id, name, ...rest } = params;
console.log("-------update-------");
console.log(id); // "1"
console.log(name); // "Created Product 1"
console.log(rest); // { foo: "bar" }
console.log("-------/update-------");

return Promise.resolve({ success: true });
},
};

Source (https://refine.dev/docs/audit-logs/audit-log-provider/#create) 3. Verify CRUD Operations: Ensure that your CRUD operations are correctly set up and that they are indeed being executed. The AuditLogProvider hooks into these operations, so if they are not being called, the provider won't be triggered. 4. Check for Errors: Look for any errors in the console that might indicate why the AuditLogProvider is not being triggered. There might be issues with the setup or other parts of your application that prevent the provider from working correctly. 5. Use useLogList Hook: You can use the useLogList hook to retrieve audit logs for your resources in your components. This can help verify if the logs are being created and retrieved correctly.
import { useLogList } from "@refinedev/core";

const productsAuditLogResults = useLogList({
resource: "products",
});

import { useLogList } from "@refinedev/core";

const productsAuditLogResults = useLogList({
resource: "products",
});

[Source](https://refine.dev/docs/guides-concepts/general-concepts/# ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5w ago
<@356390195681296387> kapa.ai is still learning and improving, please let us know how it did by reacting below
evident-indigo
evident-indigo5w ago
Is there a way include list and show in audit logs? @kapa.ai Is there a way include list and show in audit logs?
kapa.ai
kapa.ai5w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, you can include list and show actions in your audit logs by customizing the AuditLogProvider to handle these actions. Here’s how you can do it: 1. Extend the AuditLogProvider: Modify your AuditLogProvider to include list and show actions. You can add these actions in the create method to log them appropriately.
export const auditLogProvider: AuditLogProvider = {
get: async (params) => {
const { resource, meta, action, author, metaData } = params;
return {};
},
create: (params) => {
const { resource, meta, action, author, data, previousData } = params;

console.log("-------create-------");
console.log(resource); // "products", "posts", etc.
console.log(meta); // { id: "1" }, { id: "2" }, etc.
console.log(action); // "create", "update", "delete", "list", "show"
console.log(author); // { id: "1", name: "John Doe" }
console.log(data); // { name: "Product 1", price: 100 }
console.log(previousData); // { name: "Product 1", price: 50 }
console.log("-------/create-------");

return Promise.resolve({ success: true });
},
update: async (params) => {
const { id, name, ...rest } = params;
console.log("-------update-------");
console.log(id); // "1"
console.log(name); // "Created Product 1"
console.log(rest); // { foo: "bar" }
console.log("-------/update-------");

return Promise.resolve({ success: true });
},
};

export const auditLogProvider: AuditLogProvider = {
get: async (params) => {
const { resource, meta, action, author, metaData } = params;
return {};
},
create: (params) => {
const { resource, meta, action, author, data, previousData } = params;

console.log("-------create-------");
console.log(resource); // "products", "posts", etc.
console.log(meta); // { id: "1" }, { id: "2" }, etc.
console.log(action); // "create", "update", "delete", "list", "show"
console.log(author); // { id: "1", name: "John Doe" }
console.log(data); // { name: "Product 1", price: 100 }
console.log(previousData); // { name: "Product 1", price: 50 }
console.log("-------/create-------");

return Promise.resolve({ success: true });
},
update: async (params) => {
const { id, name, ...rest } = params;
console.log("-------update-------");
console.log(id); // "1"
console.log(name); // "Created Product 1"
console.log(rest); // { foo: "bar" }
console.log("-------/update-------");

return Promise.resolve({ success: true });
},
};

2. Trigger list and show Actions: Ensure that your application triggers these actions. You can manually call the create method of the AuditLogProvider when these actions occur.
// Example of triggering a list action
auditLogProvider.create({
resource: "products",
action: "list",
author: { id: "1", name: "John Doe" },
data: {}, // You can include any relevant data here
meta: { id: "1" },
});

// Example of triggering a show action
auditLogProvider.create({
resource: "products",
action: "show",
author: { id: "1", name: "John Doe" },
data: {}, // You can include any relevant data here
meta: { id: "1" },
});

// Example of triggering a list action
auditLogProvider.create({
resource: "products",
action: "list",
author: { id: "1", name: "John Doe" },
data: {}, // You can include any relevant data here
meta: { id: "1" },
});

// Example of triggering a show action
auditLogProvider.create({
resource: "products",
action: "show",
author: { id: "1", name: "John Doe" },
data: {}, // You can include any relevant data here
meta: { id: "1" },
});

3. Integrate with useLogList: Use the useLogList hook to retrieve and display these logs in your components.
import { useLogList } from "@refinedev/core";

const productsAuditLogResults = useLogList({
resource: "products",
});

import { useLogList } from "@refinedev/core";

const productsAuditLogResults = useLogList({
resource: "products",
});

By following these steps, you can include list and show actions in your audit logs, ensuring that all relevant user activities are logged and can be reviewed later. For more detailed information, you can refer to the Audit Log Provider documentation (https://refine.dev/docs/audit-logs/audit-log-provider/#creating-an-audit-log-provider) and the useLogList hook documentation (https://refine.dev/docs/audit-logs/hooks/use-log-list/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5w ago
<@356390195681296387> kapa.ai is still learning and improving, please let us know how it did by reacting below
evident-indigo
evident-indigo5w ago
Is there a way to automate this for all list and show? @kapa.ai Is there a way to automate this for all list and show?