    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     */
    017    package org.apache.logging.log4j.core.net;
    018
    019    import org.apache.logging.log4j.Level;
    020
    021    /**
    022     * The Priority used in the Syslog system. Calculated from the Facility and Severity values.
    023     */
    024    public class Priority {
    025
    026        private final Facility facility;
    027        private final Severity severity;
    028
    029        /**
    030         * The Constructor.
    031         * @param facility The Facility.
    032         * @param severity The Severity.
    033         */
    034        public Priority(final Facility facility, final Severity severity) {
    035            this.facility = facility;
    036            this.severity = severity;
    037        }
    038
    039        /**
    040         * Returns the priority value based on the Facility and Log Level.
    041         * @param facility The Facility.
    042         * @param level The Level.
    043         * @return The integer value of the priority.
    044         */
    045        public static int getPriority(final Facility facility, final Level level) {
    046            return toPriority(facility, Severity.getSeverity(level));
    047        }
    048
    049        private static int toPriority(Facility aFacility, Severity aSeverity) {
    050            return (aFacility.getCode() << 3) + aSeverity.getCode();
    051        }
    052
    053        /**
    054         * Returns the Facility.
    055         * @return the Facility.
    056         */
    057        public Facility getFacility() {
    058            return facility;
    059        }
    060
    061        /**
    062         * Returns the Severity.
    063         * @return the Severity.
    064         */
    065        public Severity getSeverity() {
    066            return severity;
    067        }
    068
    069        /**
    070         * Returns the value of this Priority.
    071         * @return the value of this Priority.
    072         */
    073        public int getValue() {
    074            return toPriority(facility, severity);
    075        }
    076
    077        @Override
    078        public String toString() {
    079            return Integer.toString(getValue());
    080        }
    081    }