---
title: Audit logs
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/audit-logs/
updated: 2026-11-18T16:57:19.575Z
---

# Audit logs

> AUDIT(), $.audit(), DEF.onAudit and the audit option of NEWACTION().

The functions that write audit records and the option that audits actions. The guide [Audit logs](../../guides/audit-logs/) explains how they work together with examples.

## API

### AUDIT([name], $, [message], [type])

Type: `method`

Writes an audit record about the request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no | `'audit'` | The log name. |
| `$` | `Object` | yes |  | The `$` of an action or a controller. |
| `message` | `String` | no |  | A message. |
| `type` | `String` | no |  | A type, for example `info` or `warning`. |

Returns: `undefined`

Builds an audit record from `$` (an action's `$` or a controller) and passes it to `DEF.onAudit`, which appends it as a line of JSON to `/logs/<name>.log` (default `audit`). The record has `userid`, `createdby`, `sessionid` (from `user.sessionid`, `$.sessionid` or the controller), `ip`, `ua`, `url`, `action` (the action ID), `type`, `message`, `instance` (cluster ID), `app` (`CONF.url` or `CONF.name`) and `dtcreated`. `data` holds `{ params, query, model, files }` as JSON, where `files` lists the uploaded files with `name`, `filename`, `size`, `width` and `height`; the fields `password`, `token`, `accesstoken`, `access_token` and `pin` are left out. With `CONF.$tapilogger`, `CONF.$tapi` and `CONF.secret_totalapi` the record goes to the Total.js API logger instead. `F.audit()` is the same function.

Example: Log a removal
```js
AUDIT('orders', $, 'Order removed', 'warning');
```

### $.audit([message], [type])

Type: `method`

AUDIT() for the current $ with variables in the message.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `String` | no |  | A message with optional variables. |
| `type` | `String` | no |  | A type. |

Returns: `undefined`

The message can contain `{user.name}`, `{model.field}`, `{value.field}`, `{query.key}` and `{params.key}`; they are replaced with the values of the request. The record goes to `/logs/audit.log`.

Example: With variables
```js
$.audit('Updated product {model.name} by {user.name}');
```

### DEF.onAudit = function(name, data, $)

Type: `delegate`

Stores audit records.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The log name, or `null`. |
| `data` | `Object` | yes |  | The record. |
| `$` | `Object` | yes |  | The request. |

The default appends `data` with `dtcreated` as a line of JSON to `/logs/<name>.log` (`name` defaults to `audit`). Replace it to write records into a database.

Example: Into a table
```js
DEF.onAudit = function(name, data) {
	data.id = UID();
	data.name = name;
	data.dtcreated = NOW;
	DATA.insert('tbl_auditlog', data);
};
```

### NEWACTION({ audit: true })

Type: `config`

Audits an action after it succeeds.

`true` writes a record without a message; a string is a Tangular template rendered with `$` (`{{ $.user.name }}`, `{{ $.model.name }}`). Records go to `/logs/actions.log`.

Example: Audited action
```js
NEWACTION({
	id: 'Orders/remove',
	params: '*id:UID',
	audit: 'Removed order {{ $.params.id }}',
	action: function($) {
		$.success();
	}
});
```

### $tapilogger : true

Type: `config`

Sends audit records to the Total.js API logger.

Returns: `Boolean`

Works together with `$tapi` (default `true`) and `secret_totalapi`; then `DEF.onAudit` is not called. Default `false`.

Example: Config
```js
$tapilogger     : true
secret_totalapi : your-totalapi-token
```
