001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package com.randomnoun.common.log4j2;
018
019import org.apache.log4j.Category;
020import org.apache.log4j.Level;
021import org.apache.log4j.spi.LocationInfo;
022import org.apache.log4j.spi.LoggingEvent;
023import org.apache.log4j.spi.ThrowableInformation;
024import org.apache.logging.log4j.core.LogEvent;
025import org.apache.logging.log4j.spi.StandardLevel;
026
027// a modified form of the LogEventAdapter in log4j2, which uses the log4j 1 LoggingEvent class
028
029/**
030 * Converts a Log4j 2 LogEvent into the components needed by a Log4j 1.x LoggingEvent.
031 * This class requires Log4j 2.
032 */
033public class LogEventAdapter extends LoggingEvent {
034
035    /** generated serialVersionUID */
036        private static final long serialVersionUID = -1455395715714529922L;
037
038        public LogEventAdapter(LogEvent event) {
039        super(event.getLoggerFqcn(), Category.getInstance(event.getLoggerName()), 
040                        event.getTimeMillis(), getLevel(event), event.getMessage(), event.getThreadName(), 
041                        event.getThrown() == null ? null : new ThrowableInformation(event.getThrown()),
042                        null, new LocationInfo(event.getThrown(), event.getLoggerFqcn()), event.getContextData().toMap()); // ?
043        // this.event = event;
044    }
045
046    /**
047     * Return the level of this event. Use this form instead of directly
048     * accessing the <code>level</code> field.
049     */
050    public static Level getLevel(LogEvent event) {
051        switch (StandardLevel.getStandardLevel(event.getLevel().intLevel())) {
052            case TRACE:
053                return Level.TRACE;
054            case DEBUG:
055                return Level.DEBUG;
056            case INFO:
057                return Level.INFO;
058            case WARN:
059                return Level.WARN;
060            case FATAL:
061                return Level.FATAL;
062            case OFF:
063                return Level.OFF;
064            case ALL:
065                return Level.ALL;
066            default:
067                return Level.ERROR;
068        }
069    }
070}