Skip to content

logs.LogException

Overview

The "LogException" method of the "logs Object". It displays and outputs logs as type Exception in "Server Script".

Syntax

logs.LogException(message, method, console, syslogs)

Parameters

Parameters Type Required Description
message string Yes Specifies the error content 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
(Exception):[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.

Output Column in System Log

Destination column Output content Notes
ErrMessage The string set in the message
Method Combines the original method string with:methodformat
SysLogType 90 Fixed at 90(Exception)

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. If an exception occurs, the log is output.

JavaScript
try {
    const siteId = 2;
    const item = items.NewIssue();
    item.Title = 'About the Version Upgrade of Pleasanter';
    const ret = items.Create(siteId, item);
} catch(e) {
    // Exceptionログ
    logs.LogException(`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 each method of the logs object so that log output can be switched between debugging and production.

Functional requirements
  1. Output control by a debug flag
    logger.debug() is output only when DEBUG_MODE (bool) is set to true. When it is false, the output is completely suppressed.

  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 levels

    The following 6 methods are provided, and each delegates to the corresponding logs method.

    Method Delegates to Always output
    logger.debug() LogInfo ✗ (debugging only)
    logger.info() LogInfo ✓
    logger.warn() LogWarning ✓
    logger.userError() LogUserError ✓
    logger.systemError() LogSystemError ✓
    logger.exception() LogException ✓
JavaScript
// ============================================================
// Debug mode. Set to true to output debug() logs
// ============================================================
const DEBUG_MODE = false;
/**
 * 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,
};
// ============================================================
// Logger
// ============================================================
const logger = (function (debugMode, target) {
    /**
     * Internal common output function
     * @param {Function} logFn   - Method of the logs object
     * @param {string}   message - Log message
     * @param {string}   method  - Method name (optional)
     * @param {boolean}  forceOutput - Whether to always output regardless of debug mode
     */
    function _write(logFn, message, method, forceOutput) {
        if (!forceOutput && !debugMode) return;
        const m = method || '';
        logFn(message, m, target.console, target.syslogs);
    }
    return {
        /**
         * Debug log (output as Info only when DEBUG_MODE=true)
         * @param {string} message
         * @param {string} [method]
         */
        debug: function (message, method) {
            _write(
                function (msg, mth, con, sys) {
                    logs.LogInfo(msg, mth, con, sys);
                },
                '[DEBUG] ' + message,
                method,
                false, // Output only in debug mode
            );
        },
        /**
         * Information log (always output as Info)
         * @param {string} message
         * @param {string} [method]
         */
        info: function (message, method) {
            _write(
                function (msg, mth, con, sys) {
                    logs.LogInfo(msg, mth, con, sys);
                },
                message,
                method,
                true, // Always output
            );
        },
        /**
         * Warning log (always output as Warning)
         * @param {string} message
         * @param {string} [method]
         */
        warn: function (message, method) {
            _write(
                function (msg, mth, con, sys) {
                    logs.LogWarning(msg, mth, con, sys);
                },
                message,
                method,
                true,
            );
        },
        /**
         * User error log (always output as UserError)
         * @param {string} message
         * @param {string} [method]
         */
        userError: function (message, method) {
            _write(
                function (msg, mth, con, sys) {
                    logs.LogUserError(msg, mth, con, sys);
                },
                message,
                method,
                true,
            );
        },
        /**
         * System error log (always output as SystemError)
         * @param {string} message
         * @param {string} [method]
         */
        systemError: function (message, method) {
            _write(
                function (msg, mth, con, sys) {
                    logs.LogSystemError(msg, mth, con, sys);
                },
                message,
                method,
                true,
            );
        },
        /**
         * Exception log (always output as Exception)
         * Intended to be used in a catch block.
         * @param {Error|string} error   - Error object or message string
         * @param {string}       [method]
         */
        exception: function (error, method) {
            var message;
            if (error && typeof error === 'object' && error.stack) {
                message = error.message + '\n' + error.stack;
            } else {
                message = String(error);
            }
            _write(
                function (msg, mth, con, sys) {
                    logs.LogException(msg, mth, con, sys);
                },
                message,
                method,
                true,
            );
        },
    };
})(DEBUG_MODE, LOG_TARGET);
// ===============
// Usage examples
// ===============
// ─── Example 1: Debug information (output only when DEBUG_MODE=true) ───
logger.debug('Processing started: siteId=' + 1000, 'MyScript');
// ─── Example 2: Normal information log (always output) ───
logger.info('Record created. Record ID: ' + 9999, 'CreateIssue');
// ─── Example 3: Warning log ───
logger.warn('The target record was not found.', 'FetchRecord');
// ─── Example 4: Error caused by the user ───
logger.userError('A required item has not been entered.', 'Validate');
// ─── Example 5: System error ───
logger.systemError('Failed to connect to the API.', 'CallApi');
// ─── Example 6: Exception handling (used together with catch) ───
try {
    const item = items.NewIssue();
    item.Title = 'Test';
    items.Create(2, abcd); // Force an exception
    logger.info('Created successfully', 'CreateIssue');
} catch (e) {
    logger.exception(e, 'CreateIssue');
}

Supported Versions

Supported Versions Body
1.4.12.0 or later Added function