Search in sources :

Example 1 with AtsConsoleLogger

use of com.axway.ats.core.log.AtsConsoleLogger in project ats-framework by Axway.

the class ActiveDbAppender method getCurrentInstance.

/**
 * This method doesn't create a new instance,
 * but returns the already created one (from log4j2) or null if there is no such.
 *
 * @return the current DB appender instance
 */
public static ActiveDbAppender getCurrentInstance() {
    if (instance == null) {
        final LoggerContext context = LoggerContext.getContext(false);
        final Configuration config = context.getConfiguration();
        Map<String, Appender> appenders = config.getAppenders();
        // maybe we should use this one to obtain Active and Passive DB appenders?
        if (appenders != null && appenders.size() > 0) {
            for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
                Appender appender = entry.getValue();
                if (appender instanceof ActiveDbAppender) {
                    instance = (ActiveDbAppender) appender;
                    isAttached = true;
                    return instance;
                }
            }
        }
    }
    if (instance != null) {
        return instance;
    }
    /*
         * Configuration in log4j2.xml file was not found for ActiveDbAppender
         * A dummy com.axway.ats.log.autodb.DbEventRequestProcessor is
         * created in order to prevent NPE when invoking methods such as getRunId()
         */
    new AtsConsoleLogger(ActiveDbAppender.class).warn("ATS Database appender is not specified in log4j2.xml file. " + "Methods such as ActiveDbAppender@getRunId() will not work.");
    isAttached = false;
    /**
     * create dummy appender configuration
     *  This configuration will be replaced with one from log4j2.xml file
     */
    DbAppenderConfiguration appenderConfig = new DbAppenderConfiguration();
    appenderConfig.setHost(DUMMY_DB_HOST);
    appenderConfig.setDatabase(DUMMY_DB_DATABASE);
    appenderConfig.setUser(DUMMY_DB_USER);
    appenderConfig.setPassword(DUMMY_DB_PASSWORD);
    instance = new ActiveDbAppender(appenderConfig);
    return instance;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) DbAppenderConfiguration(com.axway.ats.log.autodb.DbAppenderConfiguration) DbAppenderConfiguration(com.axway.ats.log.autodb.DbAppenderConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) HashMap(java.util.HashMap) Map(java.util.Map) AtsConsoleLogger(com.axway.ats.core.log.AtsConsoleLogger)

Example 2 with AtsConsoleLogger

use of com.axway.ats.core.log.AtsConsoleLogger in project ats-framework by Axway.

the class DbAppenderConfiguration method validate.

/**
 * Verify that the configuration is valid - the method will throw runtime exception if
 * the configuration is not valid
 * @throws InvalidAppenderConfigurationException if the configuration is not valid
 */
public void validate() throws InvalidAppenderConfigurationException {
    if (host == null) {
        throw new InvalidAppenderConfigurationException("host");
    }
    if (port == null) {
        new AtsConsoleLogger(getClass()).warn("Database port (\"port\" property) is not specified in log4j2.xml file, section for ATS ActiveDbAppender. " + "Assuming default value for Microsoft SQL Server databases (" + DbConnSQLServer.DEFAULT_PORT + ")");
        this.port = DbConnSQLServer.DEFAULT_PORT + "";
    }
    if (database == null) {
        throw new InvalidAppenderConfigurationException("database");
    }
    if (user == null) {
        throw new InvalidAppenderConfigurationException("user");
    }
    if (password == null) {
        throw new InvalidAppenderConfigurationException("password");
    }
    if (driver == null) {
        this.driver = DbKeys.SQL_SERVER_DRIVER_JTDS;
    }
    if (chunkSize == null) {
        this.chunkSize = AbstractDbAccess.DEFAULT_CHUNK_SIZE + "";
    }
    // despite the method name it actually is quite good in validating maxNumberLogEvents member
    this.maxNumberLogEvents = getMaxNumberLogEvents() + "";
    if (this.mode != "" && !this.mode.equalsIgnoreCase("batch")) {
        boolean explicitEnableOfLogger = false;
        if (AtsConsoleLogger.getLevel() == null) {
            explicitEnableOfLogger = true;
            AtsConsoleLogger.setLevel(Level.WARN);
        }
        new AtsConsoleLogger(getClass()).warn("Invalid value (" + this.mode + ") for ATS DB Appender paramenter 'mode'! Setting it to non-batch mode (empty string)");
        this.mode = "";
        if (explicitEnableOfLogger) {
            AtsConsoleLogger.setLevel(null);
        }
    }
}
Also used : InvalidAppenderConfigurationException(com.axway.ats.log.autodb.exceptions.InvalidAppenderConfigurationException) AtsConsoleLogger(com.axway.ats.core.log.AtsConsoleLogger)

Example 3 with AtsConsoleLogger

use of com.axway.ats.core.log.AtsConsoleLogger in project ats-framework by Axway.

the class DbEventRequestProcessor method constructUpdateRunEvent.

private void constructUpdateRunEvent() {
    try {
        if (userProvidedUpdateRunEvent != null) {
            // get current run info from database
            Run run = getLatestRun();
            // replace the missing fields, received by the latest UpdateRunEvent with the one from the DB
            String runName = (userProvidedUpdateRunEvent.getRunName() != null) ? userProvidedUpdateRunEvent.getRunName() : run.runName;
            String osName = (userProvidedUpdateRunEvent.getOsName() != null) ? userProvidedUpdateRunEvent.getOsName() : run.os;
            String productName = (userProvidedUpdateRunEvent.getProductName() != null) ? userProvidedUpdateRunEvent.getProductName() : run.productName;
            String versionName = (userProvidedUpdateRunEvent.getVersionName() != null) ? userProvidedUpdateRunEvent.getVersionName() : run.versionName;
            String buildName = (userProvidedUpdateRunEvent.getBuildName() != null) ? userProvidedUpdateRunEvent.getBuildName() : run.buildName;
            String userNote = (userProvidedUpdateRunEvent.getUserNote() != null) ? userProvidedUpdateRunEvent.getUserNote() : run.userNote;
            String hostName = (userProvidedUpdateRunEvent.getHostName() != null) ? userProvidedUpdateRunEvent.getHostName() : run.hostName;
            // construct the new pending UpdateRunEvent
            actualUpdateRunEvent = new UpdateRunEvent(userProvidedUpdateRunEvent.getLoggerFqcn(), (Logger) userProvidedUpdateRunEvent.getLogger(), runName, osName, productName, versionName, buildName, userNote, hostName);
        }
    } catch (SQLException e) {
        /*  
             * Could not obtain run info from database.
             * The exception will be logged to the console only, because the event processor is busy handling the UpdateRunEvent
             * and will not be able to handle the log message event as well
            */
        new AtsConsoleLogger(getClass()).error("Unable to update run with ID '" + eventProcessorState.getRunId() + "'", e);
    }
}
Also used : UpdateRunEvent(com.axway.ats.log.autodb.events.UpdateRunEvent) SQLException(java.sql.SQLException) Run(com.axway.ats.log.autodb.entities.Run) Logger(org.apache.logging.log4j.Logger) AtsConsoleLogger(com.axway.ats.core.log.AtsConsoleLogger) AtsConsoleLogger(com.axway.ats.core.log.AtsConsoleLogger)

Aggregations

AtsConsoleLogger (com.axway.ats.core.log.AtsConsoleLogger)3 DbAppenderConfiguration (com.axway.ats.log.autodb.DbAppenderConfiguration)1 Run (com.axway.ats.log.autodb.entities.Run)1 UpdateRunEvent (com.axway.ats.log.autodb.events.UpdateRunEvent)1 InvalidAppenderConfigurationException (com.axway.ats.log.autodb.exceptions.InvalidAppenderConfigurationException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Logger (org.apache.logging.log4j.Logger)1 Appender (org.apache.logging.log4j.core.Appender)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1