001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.reef.util.logging; 021 022import java.io.*; 023import java.nio.charset.StandardCharsets; 024import java.util.ArrayList; 025 026/** 027 * Parse logs for reporting. 028 */ 029public final class LogParser { 030 031 public static final String[] END_INDICATORS = { 032 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.BRIDGE_SETUP, 033 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_SUBMIT, 034 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_BRIDGE_SUBMIT, 035 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.DRIVER_START, 036 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_LAUNCH, 037 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_ALLOCATED, 038 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.ACTIVE_CONTEXT, 039 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.HTTP_REQUEST, 040 LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.TASK_COMPLETE 041 }; 042 043 public static final String[] START_INDICATORS = { 044 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.DRIVER_START, 045 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.BRIDGE_SETUP, 046 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_BRIDGE_SUBMIT, 047 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_SUBMIT, 048 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_ALLOCATED, 049 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_LAUNCH, 050 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.ACTIVE_CONTEXT, 051 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.HTTP_REQUEST, 052 LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.TASK_COMPLETE 053 }; 054 055 private LogParser() { 056 } 057 058 /** 059 * Get lines from a given file with a specified filter, trim the line by removing strings 060 * before removeBeforeToken and after removeAfterToken. 061 * @param fileName 062 * @param filter 063 * @return 064 * @throws IOException 065 */ 066 public static ArrayList<String> getFilteredLinesFromFile(final String fileName, 067 final String filter, 068 final String removeBeforeToken, 069 final String removeAfterToken) throws IOException{ 070 final ArrayList<String> filteredLines = new ArrayList<>(); 071 try (final BufferedReader in = new BufferedReader( 072 new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8))) { 073 String line = ""; 074 while ((line = in.readLine()) != null) { 075 if (line.trim().length() == 0) { 076 continue; 077 } 078 if (line.contains(filter)) { 079 String trimedLine; 080 if (removeBeforeToken != null) { 081 final String[] p = line.split(removeBeforeToken); 082 if (p.length > 1) { 083 trimedLine = p[p.length-1]; 084 } else { 085 trimedLine = line.trim(); 086 } 087 } else { 088 trimedLine = line.trim(); 089 } 090 if (removeAfterToken != null) { 091 final String[] p = trimedLine.split(removeAfterToken); 092 if (p.length > 1) { 093 trimedLine = p[0]; 094 } 095 } 096 filteredLines.add(trimedLine); 097 } 098 } 099 } 100 return filteredLines; 101 } 102 103 /** 104 * get lines from given file with specified filter. 105 * @param fileName 106 * @param filter 107 * @return 108 * @throws IOException 109 */ 110 public static ArrayList<String> getFilteredLinesFromFile(final String fileName, final String filter) 111 throws IOException { 112 return getFilteredLinesFromFile(fileName, filter, null, null); 113 } 114 115 /** 116 * filter array list of lines and get the last portion of the line separated by the token, like ":::". 117 * @param original 118 * @param filter 119 * @return 120 */ 121 public static ArrayList<String> filter(final ArrayList<String> original, final String filter, final String token) { 122 final ArrayList<String> result = new ArrayList<>(); 123 for (final String line : original) { 124 if (line.contains(filter)) { 125 final String[] p = line.split(token); 126 if (p.length > 1) { 127 result.add(p[p.length-1]); 128 } 129 } 130 } 131 return result; 132 } 133 134 /** 135 * find lines that contain stage indicators. The stageIndicators must be in sequence which appear in the lines. 136 * @param lines 137 * @param stageIndicators 138 * @return 139 */ 140 public static ArrayList<String> findStages(final ArrayList<String> lines, final String[] stageIndicators) { 141 final ArrayList<String> stages = new ArrayList<>(); 142 143 int i = 0; 144 for (final String line: lines) { 145 if (line.contains(stageIndicators[i])){ 146 stages.add(stageIndicators[i]); 147 if (i < stageIndicators.length - 1) { 148 i++; 149 } 150 } 151 } 152 return stages; 153 } 154 155 public static ArrayList<String> mergeStages(final ArrayList<String> startStages, final ArrayList<String> endStages) { 156 final ArrayList<String> mergeStage = new ArrayList<>(); 157 for (int i = 0; i < startStages.size(); i++) { 158 final String end = startStages.get(i).replace(LoggingScopeImpl.START_PREFIX, LoggingScopeImpl.EXIT_PREFIX); 159 if (endStages.contains(end)) { 160 mergeStage.add(startStages.get(i) + " " + end); 161 } else { 162 mergeStage.add(startStages.get(i)); 163 } 164 } 165 return mergeStage; 166 } 167}