Skip to content

logs.Log

Overview

The Log method of the "logs Object". It displays and outputs logs of the specified type in "Server Script".

Syntax

logs.Log(type, message, method, console, syslogs)

Parameters

Parameters Type Required Description
type int Yes Specifies the type of log. Possible values are as follows:
10:Info
50:Warning
60:UserError
80:SystemError
90:Exception
message string Yes Specifies the content of the error to display and output.
method string Optional. Default value is an empty string.
console bool Optional. Default value is true. Set to true to display in the browser developer tools console.
syslogs bool Optional. Default value is true. Set to true to output to the system log.

Return Value

Returns true if the log is successfully displayed and output, otherwise returns false.

Display Format in Console

Display Format:
(SysLogType):[Method]Message
Display column Display content Notes
Message The string set in the message
Method The string set in the method Brackets ([]) are not displayed if the method is not specified.
SysLogType The string displayed based on the type:
10:Info
50:Warning
60:UserError
80:SystemError
90:Exception

Output Column in System Log

Output destination column Output content Notes
Comments The string set in the message For types 10(Info)、50(Warning)、60(UserError)
ErrMessage The string set in the message For types 80(SystemError)、90(Exception)
Method Combines the original method string with :method format.:methodformat
SysLogType type

Usage Examples

In the following example, a record with the title "About the Version Upgrade of Pleasanter" is created in a "temporary table" under Site ID 2. Based on the result, logs are output using logs.Log.

JavaScript
try {
    const siteId = 2;
    const item = items.NewIssue();
    item.Title = 'About the Version Upgrade of Pleasanter';
    const ret = items.Create(siteId, item);
    if (ret) {
        // Info log
        logs.Log(10, `Record created. Record ID:${item.IssueId}`); 
    } else {
        // SystemError log
        logs.Log(80, `Failed to create the record. Site ID:${siteId}`); 
    }
} catch(e) {
    // Exception log
    logs.Log(90, `Exception occurred\n ${e.stack}`);
}

Code Samples

1. A general-purpose logging function

This is a sample code of a general-purpose logger that wraps the logs.Log method so that log output can be switched between debugging and production.

Functional requirements

  1. Output control by a debug flag

    DEBUG_MODE (bool) controls the output of Info (type=10). When it is false, Info is suppressed, and when it is true, Info is output. Warning (50) and above are always output regardless of DEBUG_MODE.

  2. Centralized management of output destinations

    With LOG_TARGET (bool values for console / syslogs), output to the console and to the system log can each be turned on or off individually. The settings apply to the whole script at once.

  3. Log type constants

    A constant object LOG_TYPE corresponding to the type argument of logs.Log is provided to prevent mistakes caused by writing numbers directly.

    Constant Value Log type
    LOG_TYPE.INFO 10 Info
    LOG_TYPE.WARNING 50 Warning
    LOG_TYPE.USER_ERROR 60 UserError
    LOG_TYPE.SYSTEM_ERROR 80 SystemError
    LOG_TYPE.EXCEPTION 90 Exception
  4. Two methods

    Method Description DEBUG_MODE=false DEBUG_MODE=true
    logEx.log(type, message, method) A thin wrapper of logs.Log. Only type=10 is suppressed Only Warning and above are output All are output
    logEx.debug(message, method) For debugging only. Output as Info (10) Not output Output
  5. Passing the method parameter through

    The method argument of each method can be passed as it is to the method parameter of logs.Log.

JavaScript
// ============================================================
// Debug mode. Set to true to output debug() and type=10 (Info) logs
// ============================================================
const DEBUG_MODE = true;
/**
 * Control of output destinations
 *   console : whether to output to the console of the browser's developer tools
 *   syslogs : whether to output to the system log (SysLogs table)
 */
const LOG_TARGET = {
    console: true,
    syslogs: true,
};
// ============================================================
// Log type constants (correspond to the type argument of logs.Log)
// ============================================================
const LOG_TYPE = {
    INFO: 10,
    WARNING: 50,
    USER_ERROR: 60,
    SYSTEM_ERROR: 80,
    EXCEPTION: 90,
};
// ============================================================
// Logger
// ============================================================
const logEx = (function (debugMode, target) {
    /**
     * When DEBUG_MODE=false, Info (type=10) is suppressed.
     * Warning and above (type>=50) are always output.
     */
    function _isOutputAllowed(type) {
        if (type === LOG_TYPE.INFO && !debugMode) return false;
        return true;
    }
    return {
        /**
         * A method that wraps logs.Log as it is.
         * type=10 (Info) is output only when DEBUG_MODE=true.
         * type=50 and above (Warning/UserError/SystemError/Exception) are always output.
         *
         * @param {number} type    - Log type (LOG_TYPE constants recommended)
         * @param {string} message - Log message
         * @param {string} [method] - Method name (optional)
         */
        log: function (type, message, method) {
            if (!_isOutputAllowed(type)) return;
            const m = method || '';
            logs.Log(type, message, m, target.console, target.syslogs);
        },
        /**
         * Log for debugging only. Output as Info (type=10) only when DEBUG_MODE=true.
         * Use it for output that is not needed in production, such as checking variable values or tracing processing.
         *
         * @param {string} message
         * @param {string} [method]
         */
        debug: function (message, method) {
            if (!debugMode) return;
            const m = method || '';
            logs.Log(
                LOG_TYPE.INFO,
                '[DEBUG] ' + message,
                m,
                target.console,
                target.syslogs,
            );
        },
    };
})(DEBUG_MODE, LOG_TARGET);
// ======================
// Usage examples
// ======================
// ─── Example 1: Debug log (output only when DEBUG_MODE=true) ───
logEx.debug('Processing started: siteId=' + 1000, 'MyScript');
// ─── Example 2: INFO log (output only when DEBUG_MODE=true) ───
logEx.log(LOG_TYPE.INFO, 'Record retrieved.', 'FetchRecord');
// ─── Example 3: WARNING log (always output) ───
logEx.log(
    LOG_TYPE.WARNING,
    'The target record was not found.',
    'FetchRecord',
);
// ─── Example 4: UserError log (always output) ───
logEx.log(LOG_TYPE.USER_ERROR, 'A required item has not been entered.', 'Validate');
// ─── Example 5: SystemError log (always output) ───
logEx.log(LOG_TYPE.SYSTEM_ERROR, 'Failed to connect to the API.', 'CallApi');
// ─── Example 6: Exception log (used together with catch) ───
try {
    const item = items.NewIssue();
    item.Title = 'Test';
    items.Create(2, abcd); // Force an exception
    logEx.log(LOG_TYPE.INFO, 'Created successfully', 'CreateIssue');
} catch (e) {
    logEx.log(LOG_TYPE.EXCEPTION, e.message + '\n' + e.stack, 'CreateIssue');
}
// ─── Example 7: A number can also be specified directly ───
logEx.log(50, 'Warning log with the type specified directly', 'DirectType');

Supported Versions

Supported Versions Body
1.4.12.0 or later Added function