Back to Refine

useLog Hook | Custom Audit Logging in Refine v4

documentation/versioned_docs/version-4.xx.xx/audit-logs/hooks/use-log/index.md

3.25.05.3 KB
Original Source

If you need to create or update an audit log, you can use Refine's useLog hook. This hook will return two mutations called log and rename

tsx
import { useLog } from "@refinedev/core";

const { log, rename } = useLog();

log

The log mutation is used to create an audit log event using the create method from auditLogProvider under the hood.

tsx
import { useLog } from "@refinedev/core";

const { log } = useLog();
const { mutate } = log;

mutate({
  resource: "posts",
  action: "create",
  author: {
    username: "admin",
  },
  data: {
    id: 1,
    title: "New post",
  },
  meta: {
    id: 1,
  },
});

Properties

PropertyType
resource <PropTag asterisk />string
action <PropTag asterisk />string
authorRecord<string, any>
metaRecord<string, any>
dataRecord<string, any>
previousDataRecord<string, any>

Type Parameters

PropertyDescriptionTypeDefault
TDataResult data of the mutation. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError
TVariablesValues for mutation function{}{}

Return value

DescriptionType
Result of the react-query's useMutationUseMutationResult<{ data: TData}, TError, { id: BaseKey; name: string; }, unknown>

rename

The rename mutation is used to update an audit log event using the update method from auditLogProvider under the hood.

tsx
import { useLog } from "@refinedev/core";

const { rename } = useLog();
const { mutate } = rename;

mutate({
  id: 1,
  name: "Updated Name",
});

Properties

PropertyType
id <PropTag asterisk />BaseKey
name <PropTag asterisk />string

Type Parameters

PropertyDescriptionTypeDefault
TDataResult data of the mutation. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError
TVariablesValues for mutation function{}{}

Return value

DescriptionType
Result of the react-query's useMutationUseMutationResult<{ data: TData}, TError, { id: BaseKey; name: string; }, unknown>