Spanish Total.js
English
Documentation For AI agents
Contents

Audit logs

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

.md GitHub
On this page

The functions that write audit records and the option that audits actions. The guide Audit logs explains how they work together with examples.

API

method

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

Writes an audit record about the request.
ParameterTypeDescription
name? String The log name. Default: 'audit'
$ Object The $ of an action or a controller.
message? String A message.
type? String 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.

Log a removal
AUDIT('orders', $, 'Order removed', 'warning');
method

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

AUDIT() for the current $ with variables in the message.
ParameterTypeDescription
message? String A message with optional variables.
type? String 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.

With variables
$.audit('Updated product {model.name} by {user.name}');
delegate

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

Stores audit records.
ParameterTypeDescription
name String The log name, or null.
data Object The record.
$ Object 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.

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

NEWACTION({ audit: true })

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.

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

$tapilogger : true

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.

Config
$tapilogger     : true
secret_totalapi : your-totalapi-token
Made in Europe