java 代码:
package com.sohu.smc.web; /** * Created by on 2015/4/17. */ import com.sohu.smc.data.utils.MyStringUtils; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServlet; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.ContextHandler; public class SgifLogWeb { // private final static org.slf4j.Logger logger = LoggerFactory.getLogger(SgifLogWeb.class); private final static org.slf4j.Logger logger = LoggerFactory.getLogger(SgifLogWeb.class); /** * <pre> * @param args * </pre> */ final static int port = 11229; public static void main(String[] args) { try { logger.info("start server port is:"+port); Server server = new Server(port); ServletContextHandler context = new ServletContextHandler( ServletContextHandler.NO_SESSIONS); context.setContextPath("/"); server.setHandler(context); context.addServlet(new ServletHolder(new SearchServlet()), "/s.gif"); server.start(); server.join(); } catch (Exception e) { e.printStackTrace(); } } } class SearchServlet extends HttpServlet { private final static org.slf4j.Logger logger = LoggerFactory.getLogger("logmsg"); private static final long serialVersionUID = -4012838481920999524L; /** * 写在这里的代码都是 POST 请求 */ @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); StringBuilder buffer = new StringBuilder(); BufferedReader reader=null; try{ reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8")); String line=null; while((line = reader.readLine())!=null){ buffer.append(line+'\n'); } }catch(Exception e){ e.printStackTrace(); }finally{ if(null!=reader){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } String[] arrays =buffer.toString().split("\n"); String dateString = MyStringUtils.getdatestring(); for(String dada :arrays) { logger.info(dateString+"\t"+dada); } } /** * 写在这里的代码都是 GET 请求 */ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String query = request.getParameter("query"); String result = "welcome to my server. It's a GET request."; if (null != query && !query.trim().equals("")) { result = query + ", " + result; } } } /* class HelloHandler extends AbstractHandler { private final static org.slf4j.Logger logger = LoggerFactory.getLogger("logmsg"); @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { */ /** * <pre> * 从 URL 里面得到传递过来的参数: * http://localhost:8080/search?query=hello * 如果你需要传递更多的参数,可以这么写: * http://localhost:8080/search?query=hello&name=ZhangLili * 从这里开始,你可以写自己的代码逻辑。 * * [注意:GET方法的请求,URL 的最大长度是 1024个字节] * </pre> *//* java.io.BufferedInputStream bis=new java.io.BufferedInputStream(request.getInputStream()); byte read[] = new byte[1024*1024]; String msg=""; while( ( bis.read(read, 0, 1024*1024)) != -1 ) { msg+=new String(read,0,1024*1024); } logger.info(msg); */ /* String query = request.getParameter("query"); // String name = request.getParameter("name"); String result = "welcome to my server."; if (null != query && query.equals("hello")) { result = query + ", " + result; } // 将服务器处理后的结果返回给调用URL的客户端 print(baseRequest, response, result);*//* } */ /** * <pre> * @param baseRequest * @param response * @param result 需要返回给客户的结果 * @throws IOException * 将结果 result 返回给客户 * </pre> *//* private void print(Request baseRequest, HttpServletResponse response, String result) throws IOException { response.setContentType("text/json;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println(result); } }*/
logback 配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
<property name="LOG_HOME" value="/opt/scm/sgifdata" />
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- 日志输出编码 -->
<Encoding>UTF-8</Encoding>
<layout class="ch.qos.logback.classic.PatternLayout">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n</pattern>
</layout>
</appender>
<!-- 按照每天生成日志文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Encoding>UTF-8</Encoding>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名-->
<FileNamePattern>${LOG_HOME}/sgif.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
<MaxHistory>48</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%msg%n</pattern>
</layout>
</appender>
<logger name="logmsg" level="INFU" additivity="false">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</logger>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
maven:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.sohu.smc</groupId>
<artifactId>realtime-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>data-platform-controler</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>3.1.4.RELEASE</spring-version>
<jetty-version>8.1.5.v20120716</jetty-version>
</properties>
<description>all kinds of consumers, it's a worker running always.</description>
<dependencies>
<!-- JETTY DEPENDENCIES -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20131018</version>
</dependency>
<!-- <dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>realtime-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>-->
<dependency>
<artifactId>jackson-mapper-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo2-api</artifactId>
<version>2.3-eb</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxp-api</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>3part-commons-io</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>3part-commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>smc-flume</artifactId>
</dependency>
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<!-- 使用该模块中封装的NSQ接口 -->
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>smc-common-nsq</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>smc-common-nsq-factory</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
<!--KAFKA8-->
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>smc-common-kafka8</artifactId>
<version>1.5-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 使用该模块中封装的Kafka生产者接口
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>smc-common-kafka</artifactId>
<version>1.6-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
</dependency>
<!-- leveldbjni -->
<dependency>
<groupId>org.fusesource.leveldbjni</groupId>
<artifactId>leveldbjni-all</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>smc-common-metrics</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>smc-common-scribe</artifactId>
<version>1.5-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- <dependency>
<groupId>com.sohu.smc</groupId>
<artifactId>realtime-userinfo</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>-->
<dependency>
<groupId>org.tarantool</groupId>
<artifactId>smc-tarantool-connector</artifactId>
<version>0.1.5</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yaml</include>
<include>**/*.xsl</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.class</exclude>
</excludes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>distribution.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>online</id>
<activation>
<property>
<name>profile</name>
<value>online</value>
</property>
</activation>
<properties>
<profile.active>online</profile.active>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>develop</id>
<properties>
<profile.active>develop</profile.active>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>