Last active
October 30, 2018 08:29
-
-
Save katanagari7c1/5b8f77e8910df227e7178afafd67534f to your computer and use it in GitHub Desktop.
Upgrade Openbravo+modules to use log4j2 Logger classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo -e "Migrating java files to log4j 2.x API" | |
echo -e "=====================================" | |
# Check if we are in Openbravo root | |
CHECKDIR=$(ls -1 | grep modules) | |
if [ "$CHECKDIR" != "modules" ] | |
then | |
echo "This script should be executed inside Openbravo root directory" | |
exit 1 | |
fi | |
# Check if there's a Logger not using their own class name as the logger name | |
# but using a custom string instead: | |
# private static Logger log = Logger.getLogger("whatever"); | |
# | |
function file_has_logger_with_custom_name { | |
CUSTOM_LOGGER=$(grep "Logger\(Factory\)\?\.getLogger(\"[[:print:]]*\")" $1) | |
if [[ -n $CUSTOM_LOGGER ]]; | |
then | |
echo "custom" | |
fi | |
} | |
# Replaces log4j 1.x imports with the 2.x packages | |
# Also updates the logger creation method and now uses LogManager.getLogger() | |
# No need of passing the class :) | |
# | |
function update_logger_creation_and_imports { | |
sed -i -r -e 's/import org.apache.log4j.Logger;/import org.apache.logging.log4j.LogManager;\nimport org.apache.logging.log4j.Logger;/g' $1 | |
sed -i -r -e 's/import org.slf4j.Logger;/import org.apache.logging.log4j.Logger;/g' $1 | |
sed -i -r -e 's/import org.slf4j.LoggerFactory;/import org.apache.logging.log4j.LogManager;/g' $1 | |
sed -i -r -e ':a;N;$!ba;s/Logger(Factory)?([\n\t ])*.getLogger\((this.getClass\(\)|[^)]+)\)/LogManager.getLogger\(\)/g' $1 | |
} | |
function print_custom_logger_warning { | |
echo -e "\n\033[0;33mWARNING:\033[0mThe following file has a custom named logger. Please upgrade it manually." | |
echo -e "$1\n" | |
} | |
function print_warnings { | |
if [[ $1 =~ "custom" ]]; then | |
print_custom_logger_warning $2 | |
fi | |
} | |
# Find all .java files in source files, and if no custom logger name is used | |
# migrate automagically | |
# | |
EDITED_FILES=0 | |
for f in src src-util src-core src-test src-trl src-wad modules | |
do | |
echo -e "=== Looking for sources in $f ===" | |
JAVA_FILES=$(find $f -iname *.java) | |
for j in $JAVA_FILES | |
do | |
WARNINGS="" | |
WARNINGS+=$(file_has_logger_with_custom_name $j) | |
if [[ -z $WARNINGS ]]; | |
then | |
update_logger_creation_and_imports $j | |
EDITED_FILES=$((EDITED_FILES+1)) | |
else | |
print_warnings $WARNINGS $j | |
fi | |
done | |
done | |
echo -e "Edited $EDITED_FILES files. Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment