From 20c1d4655937f9b0c1c0d90d3a7b59772dd2decb Mon Sep 17 00:00:00 2001 From: "leon.li" Date: Fri, 4 Dec 2015 09:28:06 +0800 Subject: [PATCH] add log formatter --- .../slb/agent/core/task/LogFormatter.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 slb-agent/src/main/java/com/dianping/platform/slb/agent/core/task/LogFormatter.java diff --git a/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/task/LogFormatter.java b/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/task/LogFormatter.java new file mode 100644 index 0000000..37140ab --- /dev/null +++ b/slb-agent/src/main/java/com/dianping/platform/slb/agent/core/task/LogFormatter.java @@ -0,0 +1,56 @@ +package com.dianping.platform.slb.agent.core.task; + +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Map; + +/** + * dianping.com @2015 + * slb - soft load balance + *

+ * Created by leon.li(Li Yang) + */ +@Service +public class LogFormatter { + + public static final String NEW_LINE = "\r\n"; + + public static final String CHUNK_TERMINATOR = "\r\n--9ed2b78c112fbd17a8511812c554da62941629a8--\r\n"; + + public static final String LOG_TERMINATOR = "\r\n--255220d51dc7fb4aacddadedfe252a346da267d4--\r\n"; + + public void writeHeader(OutputStream logOut, Map headers) throws IOException { + StringBuilder sb = new StringBuilder(); + + if (headers != null) { + for (Map.Entry headEntry : headers.entrySet()) { + sb.append(headEntry.getKey()); + sb.append(": "); + sb.append(headEntry.getValue()); + sb.append(NEW_LINE); + } + } + + if (sb.length() > 0) { + sb.append(NEW_LINE); + } else { + sb.append(NEW_LINE); + sb.append(NEW_LINE); + } + + logOut.write(sb.toString().getBytes()); + } + + public void writeChunkTerminator(OutputStream logOut) throws IOException { + logOut.write(CHUNK_TERMINATOR.getBytes("ascii")); + logOut.flush(); + } + + public void writeTerminator(OutputStream logOut) throws IOException { + logOut.write(LOG_TERMINATOR.getBytes("ascii")); + logOut.flush(); + } + +}