All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.python.logging.CustomAppender Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2022, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * SPDX-License-Identifier: MPL-2.0
 */
package com.powsybl.python.logging;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.ThrowableProxyUtil;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.encoder.Encoder;
import com.powsybl.python.commons.CTypeUtil;

/**
 * Custom appender for python logging
 *
 * @author Bertrand Rix {@literal }
 */
public class CustomAppender extends AppenderBase {

    private Encoder encoder;

    @Override
    protected void append(final ILoggingEvent e) {
        LoggingCFunctions.LoggerCallback logMessage = (LoggingCFunctions.LoggerCallback) LoggingCFunctions.loggerCallback;

        String message = e.getFormattedMessage();
        IThrowableProxy throwable = e.getThrowableProxy();
        if (throwable != null) {
            message = message + CoreConstants.LINE_SEPARATOR + ThrowableProxyUtil.asString(throwable);
        }

        logMessage.invoke(PyLoggingUtil.logbackLevelToPythonLevel(e.getLevel()), e.getTimeStamp(), CTypeUtil.toCharPtr(e.getLoggerName()), CTypeUtil.toCharPtr(message));
    }

    public Encoder getEncoder() {
        return this.encoder;
    }

    public void setEncoder(Encoder encoder) {
        this.encoder = encoder;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy