All files / ide logger.ts

100% Statements 25/25
92.85% Branches 13/14
100% Functions 10/10
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                  11x       3x       6x       1x       1x       2x       3x       4x 4x 1x         3x 3x 3x             16x 11x 11x   11x 11x 1x 10x 5x   11x 10x                       11x    
/*
      Copyright (C) 2025 Sber
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
 
              http://www.apache.org/licenses/LICENSE-2.0
 
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
 */
 
import {ChangeLevelResult, SeafLogger, LogMessageFn} from '@global/logger/v2/logger.types';
import {logMessageToStringArray} from '@global/logger/v2/logMessageToStringArray';
import { changeLoggerImpl } from '@global/logger/v2/logger';
 
enum LogLevel {
    off =  0,
    error =  1,
    warn =  2,
    info =  3,
    debug =  4,
    trace =  5
}
 
/**
 * Логгер для UI
 */
class PluginLogger implements SeafLogger {
    private currentLevel: LogLevel = LogLevel.debug;
 
 
    error(tag: string, msgFn: LogMessageFn, error?: unknown): void {
        this.log(LogLevel.error, tag, msgFn, error);
    }
 
    warn(tag: string, msgFn: LogMessageFn, error?: unknown): void {
        this.log(LogLevel.warn, tag, msgFn, error);
    }
 
    info(tag: string, msgFn: LogMessageFn, error?: unknown): void {
        this.log(LogLevel.info, tag, msgFn, error);
    }
 
    debug(tag: string, msgFn: LogMessageFn, error?: unknown): void {
        this.log(LogLevel.debug, tag, msgFn, error);
    }
 
    trace(tag: string, msgFn: LogMessageFn, error?: unknown): void {
        this.log(LogLevel.trace, tag, msgFn, error);
    }
 
    getLevelName(): string {
        return LogLevel[this.currentLevel];
    }
 
    setLevel(level: string): ChangeLevelResult {
        const normalizedLevel = LogLevel[level.toLowerCase()];
        if (!normalizedLevel && normalizedLevel !== LogLevel.off) { // т.к. off = 0 то его проверяем отдельно
            return {
                isSuccess: false,
                message: `Unknown log level: ${level}. Available levels: ${Object.keys(LogLevel)}`
            };
        }
        this.currentLevel = normalizedLevel;
        this.log(LogLevel.info, 'PluginLogger', () => `setLevel Log level changed to: ${LogLevel[normalizedLevel]}`);
        return {
            isSuccess: true,
            message: `log level changed to ${normalizedLevel}`
        };
    }
 
    private log(level: LogLevel, tag: string, msgFn: LogMessageFn, error?: unknown) {
        if (this.currentLevel >= level) {
            const levelTag = LogLevel[level];
            const message = (logMessageToStringArray(msgFn) || [])
                .join('\n');
            let errorStack: string = undefined;
            if(error instanceof Error){
                errorStack = `| errorStack: ${JSON.stringify(error.stack)}`;
            } else if (error) {
                errorStack = `| errorStack: ${JSON.stringify(error)}`;
            }
            if (window.$PAPI) {
                window.$PAPI.sendLog({
                    level: levelTag,
                    tag: tag,
                    message: message,
                    errorStack: errorStack
                });
            }
        }
    }
}
 
export function initPluginLogger() {
    changeLoggerImpl(new PluginLogger());
}