Mercurial Hosting > luan
changeset 205:3918f92de5fe
add logging component
git-svn-id: https://luan-java.googlecode.com/svn/trunk@206 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 07 Jul 2014 09:37:02 +0000 |
parents | cee6581b6f56 |
children | 77365c9fdfe4 |
files | build.sh dist/luan-core-trunk.jar dist/luan-logging-trunk.jar dist/luan-web-trunk.jar logging/ext/log4j-1.2.17.jar logging/src/luan/modules/logging/Logging.luan |
diffstat | 6 files changed, 106 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
diff -r cee6581b6f56 -r 3918f92de5fe build.sh --- a/build.sh Sun Jul 06 04:48:23 2014 +0000 +++ b/build.sh Mon Jul 07 09:37:02 2014 +0000 @@ -23,3 +23,11 @@ javac -classpath $CLASSPATH `find $SRC -name *.java` cd $SRC jar cvf $HOME/dist/luan-web-$VERSION.jar `find . -name *.class -o -name *.luan` + +cd $HOME +SRC=logging/src +CLASSPATH=$HOME/core/src:$HOME/$SRC +for i in $HOME/web/ext/* ; do CLASSPATH=$CLASSPATH:$i ; done +#javac -classpath $CLASSPATH `find $SRC -name *.java` +cd $SRC +jar cvf $HOME/dist/luan-logging-$VERSION.jar `find . -name *.class -o -name *.luan`
diff -r cee6581b6f56 -r 3918f92de5fe dist/luan-core-trunk.jar Binary file dist/luan-core-trunk.jar has changed
diff -r cee6581b6f56 -r 3918f92de5fe dist/luan-logging-trunk.jar Binary file dist/luan-logging-trunk.jar has changed
diff -r cee6581b6f56 -r 3918f92de5fe dist/luan-web-trunk.jar Binary file dist/luan-web-trunk.jar has changed
diff -r cee6581b6f56 -r 3918f92de5fe logging/ext/log4j-1.2.17.jar Binary file logging/ext/log4j-1.2.17.jar has changed
diff -r cee6581b6f56 -r 3918f92de5fe logging/src/luan/modules/logging/Logging.luan --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/logging/src/luan/modules/logging/Logging.luan Mon Jul 07 09:37:02 2014 +0000 @@ -0,0 +1,98 @@ +import "Java" +import "org.apache.log4j.Logger" +import "org.apache.log4j.EnhancedPatternLayout" +import "org.apache.log4j.ConsoleAppender" +import "org.apache.log4j.Level" +import "org.apache.log4j.RollingFileAppender" + + +layout = "%d %-5p %c - %m%n" + +level = "INFO" + +console = "System.err" -- or "System.out" or set to nil for no console + +file = nil -- set to file name if you want logging to a file + +max_file_size = "10MB" + + +log4j_root_logger = Logger.getRootLogger() + +local function to_level(level) + return level and Level.toLevel(level) +end + +function log_to_file(file) + local appender = RollingFileAppender.new(ptn_layout, file) + appender.setMaxFileSize(max_file_size) + print("getMaximumFileSize",appender.getMaximumFileSize()) + log4j_root_logger.addAppender(appender) + return appender +end + +function init() + log4j_root_logger.removeAllAppenders() + log4j_root_logger.setLevel( to_level(level) ) + ptn_layout = EnhancedPatternLayout.new(layout) + + if console ~= nil then + console_appender = ConsoleAppender.new(ptn_layout,console) + log4j_root_logger.addAppender(console_appender) + else + console_appender = nil + end + + if file ~= nil then + file_appender = log_to_file(file) + else + file_appender = nil + end +end + +init() + + +local function to_luan_logger(log4j_logger) + local tbl = {} + + function tbl.error(obj) + log4j_logger.error( to_string(obj) ) + end + + function tbl.warn(obj) + log4j_logger.warn( to_string(obj) ) + end + + function tbl.info(obj) + log4j_logger.info( to_string(obj) ) + end + + function tbl.debug(obj) + log4j_logger.debug( to_string(obj) ) + end + + function tbl.get_level() + local level = log4j_logger.getLevel() + return level and level.toString() + end + + function tbl.get_effective_level() + local level = log4j_logger.getEffectiveLevel() + return level and level.toString() + end + + function tbl.set_level(level) + log4j_logger.setLevel( to_level(level) ) + end + + return tbl +end + +function logger(name) + return to_luan_logger( Logger.getLogger(name) ) +end + +function root_logger() + return to_luan_logger( Logger.getRootLogger() ) +end