Add runtime log rolling.

- Default template updated to trigger log rolling,
  when the file exceeds 100MB.
   ${LOGBASE}.%i.log.gz
  This may relax ONOS-1336 to some extent.
- Added std{out,err} and gziped log created by rolling to
  files rotated on each ONOS startup.

Change-Id: Ic0e1e7cd26eee6be6b53ea9e5faac6aff1e4fa55
diff --git a/conf/template/logback.xml.template b/conf/template/logback.xml.template
index aef06ed..e23821b 100644
--- a/conf/template/logback.xml.template
+++ b/conf/template/logback.xml.template
@@ -1,16 +1,24 @@
 <configuration scan="true" scanPeriod="1 minutes" debug="true">
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-<encoder>
-<pattern>%level [%logger:%thread] %msg%n</pattern>
-</encoder>
+  <encoder>
+    <pattern>%level [%logger:%thread] %msg%n</pattern>
+  </encoder>
 </appender>
 
-<appender name="FILE" class="ch.qos.logback.core.FileAppender">
-<file>__FILENAME__</file>
-<encoder>
-<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
-<immediateFlush>true</immediateFlush>
-</encoder>
+<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+  <file>__FILENAME__</file>
+  <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
+    <fileNamePattern>__ROLLING_PATTERN__</fileNamePattern>
+    <minIndex>1</minIndex>
+    <maxIndex>10</maxIndex>
+  </rollingPolicy>
+  <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
+    <maxFileSize>100MB</maxFileSize>
+  </triggeringPolicy>
+  <encoder>
+    <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
+    <immediateFlush>true</immediateFlush>
+  </encoder>
 </appender>
 
 <logger name="org" level="WARN"/>
@@ -19,6 +27,6 @@
 <logger name="com.hazelcast" level="INFO"/>
 
 <root level="DEBUG">
-<appender-ref ref="FILE" />
+  <appender-ref ref="FILE" />
 </root>
-</configuration>
\ No newline at end of file
+</configuration>
diff --git a/onos.sh b/onos.sh
index c1e1b2b..0eac025 100755
--- a/onos.sh
+++ b/onos.sh
@@ -86,8 +86,11 @@
 LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
 LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
 ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
+ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
+ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
+ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
 PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
-LOGS="$ONOS_LOG $PCAP_LOG"
+LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $PCAP_LOG"
 
 ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
 JMX_PORT=${JMX_PORT:-7189}
@@ -383,8 +386,9 @@
   
   # creation of logback config
   local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
-  
-  sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
+
+  sed -e "s|__FILENAME__|${ONOS_LOG}|" \
+      -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
   
   end-conf-creation ${ONOS_LOGBACK}
   
@@ -819,13 +823,22 @@
   if [ ! -d ${LOGDIR} ]; then
     mkdir -p ${LOGDIR}
   fi
-  # Backup log files
+  # Rotate log files
   for log in ${LOGS}; do
     if [ -f ${log} ]; then
       rotate-log ${log}
     fi
   done
-  
+
+  # Rotate logs rolled at runtime.
+  local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
+  for rolled_log in ${rolled_log_shellpat}; do
+    if [ -f ${rolled_log} ]; then
+      rotate-log ${rolled_log}
+      # NOTE: renamed file will end up with an extension like .log.gz.1
+    fi
+  done
+
   if [ ! -f ${ONOS_LOGBACK} ]; then
     echo "[WARNING] ${ONOS_LOGBACK} not found."
     echo "          Run \"\$ $0 setup\" to create."