- 浏览: 209152 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
谁说我不是会员:
不错,学习了。
查看java对象的内存占用情况 -
郭小小小:
solr之functionQuery(函数查询) -
LinApex:
这篇文章你忘记写了
使用redis实现自动补全 -
13534608437:
进价排名,每次往elevate.xml里面添加一条新的数据,是 ...
solr中竞价排名实现 -
飞天奔月:
不错的文章, 丝丝入扣 有条理
使用Log4j的邮件功能
上一篇文章已经安装完scribe,下面我们用java端,通过log4j 把日志写入scribe 日志系统。
一、生成scribe客户端
- 生成 java客户端api
A. 修改配置文件scribe.thrift
cd /usr/local/scribeInstall/scribe/if
vi scribe.thrift
修改scribe.thrift文件: 把 include "fb303/if/fb303.thrift" 改成
include "[thrift解压路径]/thrift-0.5.0/contrib/fb303/if/fb303.thrift"
B. 生成 java客户端api
运行命令 thrift --gen java scribe.thrift
运行后会生成一个‘gen-java’的文件夹.里面会有3个java类,封装了所有java客户端发送log所需要的api。
- 生成客户端代码所需要的jar包
A. 如果之前没设置ANT_HOME 和 PATH 请先设置这两个环境变量
export ANT_HOME=/usr/local/apache-ant-1.8.0
export PATH=$PATH:$ANT_HOME/bin
B. 生成libthrift.jar
cd /usr/local/scribeInstall/thrift-0.2.0/lib/java
ant
(如果没有错误在本文夹夹下会生成libthrift.jar)
C. 生成libfb303.jar
cd /usr/local/scribeInstall/thrift-0.2.0/contrib/fb303/java
ant
ant 执行成功后 libfb303.jar 会出现在/usr/local/scribeInstall/contrib/fb303/java/build/lib下
二、创建项目,运行测试
- 创建项目
- 编写项目
- package com.logtest;
- import org.apache.log4j.AsyncAppender;
- /**
- * log4j 的scribe appender
- * 用ScribeAppender 类连接scribe服务器,并把日志写如scribe
- * @author ninja
- */
- public class AsyncScribeAppender extends AsyncAppender {
- private String hostname;
- private String scribeHost;
- private int scribePort;
- private String scribeCategory;
- private String encoading;
- public String getHostname() {
- return hostname;
- }
- public void setHostname(String hostname) {
- this .hostname = hostname;
- }
- public String getScribeHost() {
- return scribeHost;
- }
- public void setScribeHost(String scribeHost) {
- this .scribeHost = scribeHost;
- }
- public int getScribePort() {
- return scribePort;
- }
- public void setScribePort( int scribePort) {
- this .scribePort = scribePort;
- }
- public String getScribeCategory() {
- return scribeCategory;
- }
- public void setScribeCategory(String scribeCategory) {
- this .scribeCategory = scribeCategory;
- }
- public String getEncoading() {
- return encoading;
- }
- public void setEncoading(String encoading) {
- this .encoading = encoading;
- }
- @Override
- public void activateOptions() {
- super .activateOptions();
- synchronized ( this ) {
- ScribeAppender scribeAppender = new ScribeAppender();
- scribeAppender.setLayout(getLayout());
- scribeAppender.setHostname(getHostname());
- scribeAppender.setScribeHost(getScribeHost());
- scribeAppender.setScribePort(getScribePort());
- scribeAppender.setScribeCategory(getScribeCategory());
- scribeAppender.setEncoding(getEncoading());
- scribeAppender.activateOptions();
- addAppender(scribeAppender);
- }
- }
- @Override
- public boolean requiresLayout() {
- return true ;
- }
- }
package com.logtest; import org.apache.log4j.AsyncAppender; /** * log4j 的scribe appender * 用ScribeAppender 类连接scribe服务器,并把日志写如scribe * @author ninja */ public class AsyncScribeAppender extends AsyncAppender { private String hostname; private String scribeHost; private int scribePort; private String scribeCategory; private String encoading; public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } public String getScribeHost() { return scribeHost; } public void setScribeHost(String scribeHost) { this.scribeHost = scribeHost; } public int getScribePort() { return scribePort; } public void setScribePort(int scribePort) { this.scribePort = scribePort; } public String getScribeCategory() { return scribeCategory; } public void setScribeCategory(String scribeCategory) { this.scribeCategory = scribeCategory; } public String getEncoading() { return encoading; } public void setEncoading(String encoading) { this.encoading = encoading; } @Override public void activateOptions() { super.activateOptions(); synchronized (this) { ScribeAppender scribeAppender = new ScribeAppender(); scribeAppender.setLayout(getLayout()); scribeAppender.setHostname(getHostname()); scribeAppender.setScribeHost(getScribeHost()); scribeAppender.setScribePort(getScribePort()); scribeAppender.setScribeCategory(getScribeCategory()); scribeAppender.setEncoding(getEncoading()); scribeAppender.activateOptions(); addAppender(scribeAppender); } } @Override public boolean requiresLayout() { return true; } }
- package com.logtest;
- import net.scribe.LogEntry;
- import net.scribe.scribe;
- import org.apache.log4j.WriterAppender;
- import org.apache.log4j.spi.LoggingEvent;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.transport.TFramedTransport;
- import org.apache.thrift.transport.TSocket;
- import org.apache.thrift.transport.TTransportException;
- import java.util.List;
- import java.util.ArrayList;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.net.InetAddress;
- import java.io.IOException;
- /**
- * 继承WriterAppender
- * 实现了scribe 服务器的链接和日志的发送。
- * @author ninja
- */
- public class ScribeAppender extends WriterAppender {
- private String hostname;
- private String scribeHost;
- private int scribePort;
- private String scribeCategory;
- private String encoding;
- private List<LogEntry> logEntries;
- private scribe.Client client;
- private TFramedTransport transport;
- public String getHostname() {
- return hostname;
- }
- public void setHostname(String hostname) {
- this .hostname = hostname;
- }
- public String getScribeHost() {
- return scribeHost;
- }
- public void setScribeHost(String scribeHost) {
- this .scribeHost = scribeHost;
- }
- public int getScribePort() {
- return scribePort;
- }
- public void setScribePort( int scribePort) {
- this .scribePort = scribePort;
- }
- public String getScribeCategory() {
- return scribeCategory;
- }
- public void setScribeCategory(String scribeCategory) {
- this .scribeCategory = scribeCategory;
- }
- public String getEncoding() {
- return encoding;
- }
- public void setEncoding(String encoding) {
- this .encoding = encoding;
- }
- /*
- * Activates this Appender by opening a transport to the Scribe server.
- */
- @Override
- public void activateOptions() {
- try {
- synchronized ( this ) {
- if (hostname == null ) {
- try {
- hostname = InetAddress.getLocalHost()
- .getCanonicalHostName();
- } catch (UnknownHostException e) {
- // can't get hostname
- }
- }
- System.out.println(scribeHost + scribePort + scribeCategory + encoding);
- // Thrift boilerplate code
- logEntries = new ArrayList<LogEntry>( 1 );
- TSocket sock = new TSocket( new Socket(scribeHost, scribePort));
- transport = new TFramedTransport(sock);
- TBinaryProtocol protocol = new TBinaryProtocol(transport,
- false , false );
- client = new scribe.Client(protocol, protocol);
- // This is commented out because it was throwing Exceptions for
- // no good reason.
- // transport.open();
- }
- } catch (TTransportException e) {
- e.printStackTrace();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /*
- * Appends a log message to Scribe
- */
- @Override
- public void append(LoggingEvent event) {
- synchronized ( this ) {
- try {
- String message = String.format("%s %s" , hostname, layout
- .format(event));
- LogEntry entry = new LogEntry(scribeCategory, message);
- logEntries.add(entry);
- client.Log(logEntries);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- logEntries.clear();
- }
- }
- }
- @Override
- public void close() {
- if (transport != null ) {
- transport.close();
- }
- }
- @Override
- public boolean requiresLayout() {
- return true ;
- }
- }
package com.logtest; import net.scribe.LogEntry; import net.scribe.scribe; import org.apache.log4j.WriterAppender; import org.apache.log4j.spi.LoggingEvent; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransportException; import java.util.List; import java.util.ArrayList; import java.net.Socket; import java.net.UnknownHostException; import java.net.InetAddress; import java.io.IOException; /** * 继承WriterAppender * 实现了scribe 服务器的链接和日志的发送。 * @author ninja */ public class ScribeAppender extends WriterAppender { private String hostname; private String scribeHost; private int scribePort; private String scribeCategory; private String encoding; private List<LogEntry> logEntries; private scribe.Client client; private TFramedTransport transport; public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } public String getScribeHost() { return scribeHost; } public void setScribeHost(String scribeHost) { this.scribeHost = scribeHost; } public int getScribePort() { return scribePort; } public void setScribePort(int scribePort) { this.scribePort = scribePort; } public String getScribeCategory() { return scribeCategory; } public void setScribeCategory(String scribeCategory) { this.scribeCategory = scribeCategory; } public String getEncoding() { return encoding; } public void setEncoding(String encoding) { this.encoding = encoding; } /* * Activates this Appender by opening a transport to the Scribe server. */ @Override public void activateOptions() { try { synchronized (this) { if (hostname == null) { try { hostname = InetAddress.getLocalHost() .getCanonicalHostName(); } catch (UnknownHostException e) { // can't get hostname } } System.out.println(scribeHost + scribePort + scribeCategory + encoding); // Thrift boilerplate code logEntries = new ArrayList<LogEntry>(1); TSocket sock = new TSocket(new Socket(scribeHost, scribePort)); transport = new TFramedTransport(sock); TBinaryProtocol protocol = new TBinaryProtocol(transport, false, false); client = new scribe.Client(protocol, protocol); // This is commented out because it was throwing Exceptions for // no good reason. // transport.open(); } } catch (TTransportException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* * Appends a log message to Scribe */ @Override public void append(LoggingEvent event) { synchronized (this) { try { String message = String.format("%s %s", hostname, layout .format(event)); LogEntry entry = new LogEntry(scribeCategory, message); logEntries.add(entry); client.Log(logEntries); } catch (Exception e) { e.printStackTrace(); } finally { logEntries.clear(); } } } @Override public void close() { if (transport != null) { transport.close(); } } @Override public boolean requiresLayout() { return true; } }
log4j.properties
- # 1 \u5b9a\u4e49\u4e86\u4e24\u4e2a\u8f93\u51fa\u7aef
- log4j.rootLogger = DEBUG,CONSOLE,scribe
- log4j.addivity.org.apache=true
- log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
- log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
- log4j.appender.CONSOLE.layout.ConversionPattern = %-4r [%t] %-5p %c - %m%n
- log4j.logger.com.vmars= DEBUG, scribe
- log4j.appender.scribe= com.logtest.AsyncScribeAppender
- log4j.appender.scribe.encoading=utf-8
- log4j.appender.scribe.hostname=scribe
- log4j.appender.scribe.scribeHost=192.168 . 2.221
- log4j.appender.scribe.scribePort=1463
- log4j.appender.scribe.hostname=ninja
- log4j.appender.scribe.scribeCategory=scribe
- log4j.appender.scribe.layout=org.apache.log4j.PatternLayout
- log4j.appender.scribe.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2 }: %m%n
#1 \u5b9a\u4e49\u4e86\u4e24\u4e2a\u8f93\u51fa\u7aef log4j.rootLogger = DEBUG,CONSOLE,scribe log4j.addivity.org.apache=true log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern = %-4r [%t] %-5p %c - %m%n log4j.logger.com.vmars= DEBUG, scribe log4j.appender.scribe= com.logtest.AsyncScribeAppender log4j.appender.scribe.encoading=utf-8 log4j.appender.scribe.hostname=scribe log4j.appender.scribe.scribeHost=192.168.2.221 log4j.appender.scribe.scribePort=1463 log4j.appender.scribe.hostname=ninja log4j.appender.scribe.scribeCategory=scribe log4j.appender.scribe.layout=org.apache.log4j.PatternLayout log4j.appender.scribe.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
测试客户端:
- package com.logtest;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class LogTest {
- private static Log log = LogFactory.getLog(LogTest. class );
- public static void main(String[] args) {
- log.error("this is a charactor test " );
- log.debug("这是中文测试" );
- log.fatal("fatal error 致命错误!!" );
- }
-
}
- resources.rar (1.1 MB)
- 下载次数: 101
评论
echo "hello world"|scribe_home/examples/scribe_cat test 这个命令测试,能在/tmp/scribetest/test/test_current 看到日志内容。
如果用你这个测试类运行之后没有报错,但是日志写到哪里去了?test_current 没有看到.
相关推荐
3. **Java客户端**:scribe的Java客户端库包含了与scribe服务端通信所需的类和方法,允许开发者在Java应用中集成scribe。 4. **log4j**:Apache的log4j是Java日志框架,用于记录应用程序的运行时信息,包括错误、...
在这个实例中,我们将使用Scribe-java库来实现OAuth与新浪微博的集成。 Scribe-java是一个轻量级的Java库,专门用于处理OAuth 1.0a和OAuth 2.0的身份验证流程。这个库简化了与各种OAuth服务提供商的交互,包括获取...
3. **运行 Scribe 客户端**:启动 Scribe 客户端,确保其能够正确接收和转发来自 log4j 的日志。 4. **启动 Scribe 服务器**:在中央服务器上启动 Scribe 服务,处理来自各个客户端的日志。 这样的日志系统不仅提供...
包含编译好的FacebookService, fb_status, FacebookBase, LogEntry, ResultCode, scribe类。 按照完thrift和scribe后,就不用自己生成了。
例如,创建一个 `$msg1` 对象,指定类别为 'Test',消息内容为 'a,b,c,d,e,f,g',然后使用 TSocket、TFramedTransport 和 TBinaryProtocol 创建 Thrift 客户端,向 Scribe Server 发送日志条目。 总的来说,Scribe ...
scribe-client - 为 scribe 自动生成的 Java 客户端 scribe-log4j - scribe-log4j附加程序 scribe-example - 使用示例 入门 克隆仓库 git clone https://github.com/caiiiycuk/scribe-log4j.git 将 scribe-client ...
**scribe软件** scribe是一款开源的日志收集系统,最初由Facebook开发并贡献给了开源社区。它设计的主要目标是为大规模分布式系统提供可靠、高效且可扩展的日志管理解决方案。scribe允许不同的服务将它们的日志数据...
**scribe详细安装文档** scribe是一个广泛使用的日志聚合系统,主要设计用于收集、分类和转发分布式系统的日志数据。在大型分布式环境中,如Hadoop或Facebook等,scribe扮演着至关重要的角色,它允许各个节点将日志...
- **客户端集成**: 应用程序需要使用Scribe的API或客户端库(如Python或C++)来发送日志到Scribe服务器。 - **日志流管理**: 创建和管理日志流,定义日志的分类和处理方式。 - **监控与调试**: 可以使用Scribe自带的...
结合这两个压缩包,我们可以构建一个Java应用,该应用能够从Scribe收集的日志数据中获取相关信息,然后利用Junrar库处理其中可能包含的RAR文件,例如,从日志文件中提取的归档日志数据。这种组合在处理如网络日志...
**Facebook Scribe 知识详解** Facebook Scribe 是一个分布式日志收集系统,由Facebook开源,主要用于处理大规模的日志数据。在大型分布式系统中,各个服务会产生大量的日志信息,这些信息对于监控、调试和数据分析...
《VideoScribe——创新的动画视频制作工具》 VideoScribe是一款备受赞誉的动画创作软件,主要用于快速制作吸引人的白板动画视频。标题中的“VideoScribe.7z”表明这是一个压缩包文件,包含了VideoScribe的主要应用...
scribe-1.3.5.jar
【标题】"scribe-ink-jdk-1.5.1.zip" 提供的是一个名为 "scribe-ink-jdk-1.5.1" 的软件包,可能是某个特定版本的 JDK (Java Development Kit) 实现,可能由开源社区 "scribe-ink" 开发。JDK 是开发和运行 Java 应用...
一款非常有趣的手绘动画视频制作软件,我们经常会看到一些创意动画视频,其中就有一只拿着画笔的手画出整幅画面,就像快进效果的沙画一样。这种手绘视频不仅有创意,还可以装X,自己可以制定视频内容,可以是你跟你...
**Scribe:Facebook的大数据处理框架** 在大数据领域,Facebook作为全球最大的社交网络之一,面临着海量数据的收集、存储和处理挑战。为了有效应对这些挑战,Facebook开发了名为Scribe的系统,它是一个集中式的日志...
例如,为了确保 Java 环境正确配置,需要设置 JAVA_HOME 和 PATH 环境变量。对于 MySQL,还需要安装相应的 JDBC 驱动程序以便其他应用程序能够连接到 MySQL 数据库。 #### 七、综合运用 - **数据收集与处理**:...
《VideoScribe 2.3.4:轻松创建手绘动画的神器》 VideoScribe,简称VS,是一款备受赞誉的动画制作软件,专为那些希望创作独特且引人入胜的手绘视频的用户设计。这个强大的工具以其易用性和丰富的资源库而闻名,无论...
它基于跨平台的语言Thrift进行开发,能够很好地支持Java Log4j等日志框架,并允许通过Log4j等工具将日志信息发送至Scribe服务端。Scribe的特点在于其高可靠性、可扩展性和易用性,特别适合大规模的日志处理场景。 #...
如果您想自己尝试一下,则可以以某种方式安装摄像机并花费数小时进行绘制,但是更有效的方法是使用VideoScribe。它使您可以创建自己绘制的视频演示文稿。界面的现代设计及其直观的控件使此过程尽可能简单。 该软件...