跳至主要内容

博文

目前显示的是标签为“logging”的博文

Microservice Log Practice(1): Log Log

Microservice Log Practice(1): Log Log The project tony worked on is now in a painful change: from the old monolithic application to new style microservice model. The leader gives him a job to do the new logging system, from logging the log, log collection, to log analyzing. Log in Monolithic Applications “We attach great importance to the logging system, do you understand why?” Leader asked. “Yep, the log can be used to debug the application in production environment in which we can’t set breakpoints. It can also be used to monitor the application status about the its health. We can even do the intrusion detection and related security by log.” Tony answered. "Yes, you are right. In a monolithic application, we package all functionality into a single Jar / War , then deploy it using application server like Tomcat or even Glassfish . It has some disadvantages like bad scalability, small changes leading to whole application re-deployment etc. But it also som...

Spring Boot Introduction

Spring Boot Introduction What is Spring Boot? What the usage of it and what’s the differences between Spring Boot and Spring? In this blog, we are going to solve those questions by introducing some basic idea. Project Dependency Structure First, we can see the projects hierarchy from the pom dependency: spring-boot-dependencies # list all dependencies of spring boot: include spring boot jar and some third party jar spring-boot-parent spring-boot-starters # list all starter project, which is a set of convenient dependency descriptor spring-boot-starter-xxx ( data-elastcisearch, web, etc ) # specific tech dependencies spring-boot-starter-parent # can include `spring-boot-starter-xxx` So, what is starter and boot ? Boot Boot is the project to start web application more easily with the help of: Embedded tomcat or jetty in spring-boot-starter-web Spring ApplicationContext etc Starter Starters are a set of convenient dependency descrip...

Design a Logging System?

Design a Logging System? Long long ago, when there is not any mature logging system, Tom is facing the problem of logging. The native logging system form Java SDK has some issues of class loading, in which it may cause memory leaks 1 . Requirement He discussed this problem with leader and they decided to write their own logging system with the following requirements: The logging system can be used by different package, class at the same time and we can easily differentiate them; The logging system can output to different location, for example, console, file or even network; The logging system can output formatted log content: plain text, JSON, XML etc; The logging system can have different levels to control the behavior in different environments: in development env, we can log more, but in production env, we maybe log only exception; Design Tom now begin to analyze the requirements of logging system using OO related patterns and try to make some basic...

Elasticsearch MySQL Sync Challenge (2): Event Driven

Elasticsearch MySQL Sync Challenge (2): Event Driven Tony was reminded that whether there exists some event-driven update way to sync data from MySQL to Elasticsearch yesterday, so he made more effort to this direction. Observer Pattern "As you have suggested, I have dived into more elegant way of sync data – event-driven way, or in design pattern: a observer pattern. I found three different ways to do. JDBC Logging “Whenever we use JDBC to connect to data base, JDBC can log the SQL statement of our execution. So we can enable this functionality and listening the log. We can easily detect the newly added log entry using Java NIO watch service 1 or other mature tools like FileBeat, but we need to translate the logged SQL statement to Elasticsearch query, which is a relative big work to do. Considering the work of make a simple SQL parser, I think we can deny this option” Tony finished. DB Trigger "The second way works in event-driven model is trigge...

SLF4J Introduction(2): Output

SLF4J Introduction(2): Output In this blog, we focus on how SLF4J and related logging system handle the output of logging. Layout Layout, which is maybe called pattern, is responsible to transform an log event into string with some format. The format of the log event is somewhat like printf method. There are many conversion characters to represent things like class name, date, thread, file name, stack trace, message, caller data etc. Log4j In log4j, the default conversion pattern is %r [%t] %p %c %x - %m%n % : the prefix of conversion character. If we want to print a single % sign, we can use %% r : the number of milliseconds elapsed from the construction of the layout until the creation of the logging event. t : name of thread p : logging priority c : the category of the logging event x : NDC (nested diagnostic context) m : the application supplied message associated with the logging event n : the platform dependent line separator character or c...

Logstash Learning (3): Application

In this blog, we will apply Logstash in our project. In this process, we first choose the tech stack, then solve the problem one by one. Design Because there exists many different types of input source, we have to choose the one that suitable for our system to use: business application directly write to Logstash by socket/http parse log file using Filebeat, then send to Logstash via message queue: message queue used as buffer Taking the availability and data consistency into account, we decided to use the following data flow: business app -> slf4j & logback -> file -> filebeat -> logstash -> ES -> kibana Source: SLF4J & Logback SLF4J & Logback are very powerful log library for Java that we have introduced some features of it here . Log Pattern In our scenario, we customize our project as following, which is very similar to the Spring Boot’s default logging pattern: logging.pattern. file = %d %5p --- [ %t ] %- 40.40 c{...

Logstash Learning (2): Config

In the last blog, we have introduced some concepts in Logstash: the log data flow from input to filter to output, the buffer & batch etc. In the this blog, we focus on how to setup Logstash. Settings Files After installing Logstash, we can find its settings files under /etc/logstash (in linux): logstash.yml: Logstash parameter config file log4j2.properties: Logstash logging config jvm.options: Logstash JVM config startup.options: It is used by system-install script in /usr/share/logstash/bin to build the startup script. It will setup options like user, group, service name, and service decription. logstash.yml Except the normal form of yml config file functionality, the logstash.yml file also supports bash-style interpolation of environment variables in setting values. pipeline: batch: size: ${BATCH_SIZE} delay: ${BATCH_DELAY:5} node: name: "node_ ${LS_NODE_NAME} " path: queue: "/tmp/ ${QUEUE_DIR:queue} " We can al...

SLF4J Introduction(1): Basic

Introduction SLF4J is a very commonly used log library in many Java application. It is favored because it allows the end-user to plug in the desired logging framework at deployment time. How is this feature implemented? How to use it? Let’s continue. Binding API & Implementation SLF4J library has two main parts: api and implementations. Its implementations are referred as bindings. In order to use SLF4J we have to both the api jar and bindings. We can replace slf4j bindings on our class path to switch logging frameworks. If we start a project with only slf4j-api-x.jar dependency, we will see the following warning: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" . SLF4J: Defaulting to no-operation ( NOP ) logger implementation SLF4J: See http://www .slf 4j .org /codes .html #StaticLoggerBinder for further details. This is because if no binding is found on the class path, SLF4J will default to a no-operation implementation. C...

Elasticsearch Problem Lists(2): With Spring

In last blog, we have introduced some problems about Elasticsearch basic concepts confusions and some config problems we met. Now, we come to the problems came cross when using Elasticsearch in application with the help of Spring Data Elasticsearch. With Spring After the understanding of Elasticsearch and configuration of server, we need to write code to interact with it. We choose the Spring Data Elasticsearch framework to assist our implementations. So the following is the problem we met when using Spring to access Elasticsearch. spring-boot-starter-data-elasticsearch: 1.5.3-RELEASE Elasticsearch server: 2.4.x Connection Clients When using Java to access Elasticsearch, we have two types of clients to choose to communicate with server: Transport Client: this client won’t be part of cluster, It just communicate with server Node Client: this client will be part of cluster – store data shards and respond search request In our cases, we just want to communicate...

RabbitMQ Learning (3): Example

We have talked about the basic concepts of rabbitmq and a sample of application . Now, in this blog, we will show some working code about how to config and send/receive message. (Notice this blog is mainly composed of code, you may would like to open you IDE and type/clone code and run it along the blog). Pure Java If we use pure java code, we can do like following: // set up connection CachingConnectionFactory cf = new CachingConnectionFactory( "host" , 5672 ); cf . setUsername( "timer" ); cf . setPassword( "xxx" ); cf . setVirtualHost( "timer_host" ); // set up the queue, exchange, binding on the broker RabbitAdmin admin = new RabbitAdmin(cf); Queue queue = new Queue ( "timerServer" ); admin . declareQueue( queue ); TopicExchange exchange = new TopicExchange( "timerExchange" ); admin . declareExchange(exchange); admin . declareBinding(BindingBuilder . bind( queue ) . to (exchange) . with ( "timer.*...