'2017/01/12'에 해당되는 글 8건

  1. 2017.01.12 Group 별로 상위 n개의 레코드 얻기
  2. 2017.01.12 mysql 관리 Tool
  3. 2017.01.12 Join 후 Update하기
  4. 2017.01.12 mysql Rownum 구현하기
  5. 2017.01.12 문자열 결합
  6. 2017.01.12 Toast 모양 변경하기
  7. 2017.01.12 3. log4j.xml 파일 예제
  8. 2017.01.12 2. log4j 기본 설명

** Group 별로 상위 n개의 레코드 얻기 **

 

 

Step 1: getting the top

We already know how to get a single column's value for the top country, as presented in the aforementioned post:

SELECT Continent
     , SUBSTRING_INDEX(ROUP_CONCAT(Name ORDER BY SurfaceArea DESC),',', 1) AS Name
  FROM Country
 GROUP BY Continent;
+---------------+--------------------+
| Continent     | Name               |
+---------------+--------------------+
| Asia          | China              |
| Europe        | Russian Federation |
| North America | Canada             |
| Africa        | Sudan              |
| Oceania       | Australia          |
| Antarctica    | Antarctica         |
| South America | Brazil             |
+---------------+--------------------+

Step 2: adding columns

This part is easy: just throw in the rest of the columns (again, only indicating the top country in each continent)

SELECT
 Continent,
 SUBSTRING_INDEX(
   GROUP_CONCAT(Name ORDER BY SurfaceArea DESC),
   ',', 1) AS Name,
 SUBSTRING_INDEX(
   GROUP_CONCAT(SurfaceArea ORDER BY SurfaceArea DESC),
   ',', 1) AS SurfaceArea,
 SUBSTRING_INDEX(
   GROUP_CONCAT(Population ORDER BY SurfaceArea DESC),
   ',', 1) AS Population
FROM
 Country
GROUP BY
 Continent
;

+---------------+--------------------+-------------+------------+
| Continent     | Name               | SurfaceArea | Population |
+---------------+--------------------+-------------+------------+
| Asia          | China              | 9572900.00  | 1277558000 |
| Europe        | Russian Federation | 17075400.00 | 146934000  |
| North America | Canada             | 9970610.00  | 31147000   |
| Africa        | Sudan              | 2505813.00  | 29490000   |
| Oceania       | Australia          | 7741220.00  | 18886000   |
| Antarctica    | Antarctica         | 13120000.00 | 0          |
| South America | Brazil             | 8547403.00  | 170115000  |
+---------------+--------------------+-------------+------------+

Step 3: casting

You'll notice that the Population column from this last execution is aligned to the left. This is because it is believed to be a string. The GROUP_CONCAT clause concatenates values in one string, and SUBSTRING_INDEX parses a substring. The same applies to the SurfaceArea column. We'll cast Population as UNSIGNED and SurfaceArea as DECIMAL:

SELECT
  Continent,
  SUBSTRING_INDEX(
    GROUP_CONCAT(Name ORDER BY SurfaceArea DESC),
    ',', 1) AS Name,
  CAST(
    SUBSTRING_INDEX(
      GROUP_CONCAT(SurfaceArea ORDER BY SurfaceArea DESC),
      ',', 1)
    AS DECIMAL(20,2)
    ) AS SurfaceArea,
  CAST(
    SUBSTRING_INDEX(
      GROUP_CONCAT(Population ORDER BY SurfaceArea DESC),
      ',', 1)
    AS UNSIGNED
    ) AS Population
FROM
 Country
GROUP BY
 Continent
;
+---------------+--------------------+-------------+------------+
| Continent     | Name               | SurfaceArea | Population |
+---------------+--------------------+-------------+------------+
| Asia          | China              |  9572900.00 | 1277558000 |
| Europe        | Russian Federation | 17075400.00 |  146934000 |
| North America | Canada             |  9970610.00 |   31147000 |
| Africa        | Sudan              |  2505813.00 |   29490000 |
| Oceania       | Australia          |  7741220.00 |   18886000 |
| Antarctica    | Antarctica         | 13120000.00 |          0 |
| South America | Brazil             |  8547403.00 |  170115000 |
+---------------+--------------------+-------------+------------+

Step 4: top n records

It's time to use string walking. Examples for string walking (described in the excellent SQL Cookbook) can be found here, here and here. We'll be using a numbers table: a simple table which lists ascending integer numbers. For example, you can use the following:

DROP TABLE IF EXISTS `tinyint_asc`;

CREATE TABLE `tinyint_asc` (
 `value` tinyint(3) unsigned NOT NULL default '0',
 PRIMARY KEY (value)
) ;

INSERT INTO `tinyint_asc` VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31),(32),(33),(34),(35),(36),(37),(38),(39),(40),(41),(42),(43),(44),(45),(46),(47),(48),(49),(50),(51),(52),(53),(54),(55),(56),(57),(58),(59),(60),(61),(62),(63),(64),(65),(66),(67),(68),(69),(70),(71),(72),(73),(74),(75),(76),(77),(78),(79),(80),(81),(82),(83),(84),(85),(86),(87),(88),(89),(90),(91),(92),(93),(94),(95),(96),(97),(98),(99),(100),(101),(102),(103),(104),(105),(106),(107),(108),(109),(110),(111),(112),(113),(114),(115),(116),(117),(118),(119),(120),(121),(122),(123),(124),(125),(126),(127),(128),(129),(130),(131),(132),(133),(134),(135),(136),(137),(138),(139),(140),(141),(142),(143),(144),(145),(146),(147),(148),(149),(150),(151),(152),(153),(154),(155),(156),(157),(158),(159),(160),(161),(162),(163),(164),(165),(166),(167),(168),(169),(170),(171),(172),(173),(174),(175),(176),(177),(178),(179),(180),(181),(182),(183),(184),(185),(186),(187),(188),(189),(190),(191),(192),(193),(194),(195),(196),(197),(198),(199),(200),(201),(202),(203),(204),(205),(206),(207),(208),(209),(210),(211),(212),(213),(214),(215),(216),(217),(218),(219),(220),(221),(222),(223),(224),(225),(226),(227),(228),(229),(230),(231),(232),(233),(234),(235),(236),(237),(238),(239),(240),(241),(242),(243),(244),(245),(246),(247),(248),(249),(250),(251),(252),(253),(254),(255);

The trick is to apply the same technique as used above, not for a single row, but for several rows. Here's how to present the top 5 countries:

SELECT
  Continent,
  SUBSTRING_INDEX(
    SUBSTRING_INDEX(
      GROUP_CONCAT(Name ORDER BY SurfaceArea DESC),
      ',', value),
    ',', -1)
    AS Name,
  CAST(
    SUBSTRING_INDEX(
      SUBSTRING_INDEX(
        GROUP_CONCAT(SurfaceArea ORDER BY SurfaceArea DESC),
        ',', value),
      ',', -1)
    AS DECIMAL(20,2)
    ) AS SurfaceArea,
  CAST(
    SUBSTRING_INDEX(
      SUBSTRING_INDEX(
        GROUP_CONCAT(Population ORDER BY SurfaceArea DESC),
        ',', value),
      ',', -1)
    AS UNSIGNED
    ) AS Population
FROM
  Country, tinyint_asc
WHERE
  tinyint_asc.value >= 1 AND tinyint_asc.value <= 5
GROUP BY
  Continent, value
;
+---------------+----------------------------------------------+-------------+------------+
| Continent     | Name                                         | SurfaceArea | Population |
+---------------+----------------------------------------------+-------------+------------+
| Asia          | China                                        |  9572900.00 | 1277558000 |
| Asia          | India                                        |  3287263.00 | 1013662000 |
| Asia          | Kazakstan                                    |  2724900.00 |   16223000 |
| Asia          | Saudi Arabia                                 |  2149690.00 |   21607000 |
| Asia          | Indonesia                                    |  1904569.00 |  212107000 |
| Europe        | Russian Federation                           | 17075400.00 |  146934000 |
| Europe        | Ukraine                                      |   603700.00 |   50456000 |
| Europe        | France                                       |   551500.00 |   59225700 |
| Europe        | Spain                                        |   505992.00 |   39441700 |
| Europe        | Sweden                                       |   449964.00 |    8861400 |
| North America | Canada                                       |  9970610.00 |   31147000 |
| North America | United States                                |  9363520.00 |  278357000 |
| North America | Greenland                                    |  2166090.00 |      56000 |
| North America | Mexico                                       |  1958201.00 |   98881000 |
| North America | Nicaragua                                    |   130000.00 |    5074000 |
| Africa        | Sudan                                        |  2505813.00 |   29490000 |
| Africa        | Algeria                                      |  2381741.00 |   31471000 |
| Africa        | Congo                                        |  2344858.00 |   51654000 |
| Africa        |  The Democratic Republic of the              |  1759540.00 |    5605000 |
| Africa        | Libyan Arab Jamahiriya                       |  1284000.00 |    7651000 |
| Oceania       | Australia                                    |  7741220.00 |   18886000 |
| Oceania       | Papua New Guinea                             |   462840.00 |    4807000 |
| Oceania       | New Zealand                                  |   270534.00 |    3862000 |
| Oceania       | Solomon Islands                              |    28896.00 |     444000 |
| Oceania       | New Caledonia                                |    18575.00 |     214000 |
| Antarctica    | Antarctica                                   | 13120000.00 |          0 |
| Antarctica    | French Southern territories                  |     7780.00 |          0 |
| Antarctica    | South Georgia and the South Sandwich Islands |     3903.00 |          0 |
| Antarctica    | Heard Island and McDonald Islands            |      359.00 |          0 |
| Antarctica    | Bouvet Island                                |       59.00 |          0 |
| South America | Brazil                                       |  8547403.00 |  170115000 |
| South America | Argentina                                    |  2780400.00 |   37032000 |
| South America | Peru                                         |  1285216.00 |   25662000 |
| South America | Colombia                                     |  1138914.00 |   42321000 |
| South America | Bolivia                                      |  1098581.00 |    8329000 |
+---------------+----------------------------------------------+-------------+------------+

 

'(DB) MySql > Select sql' 카테고리의 다른 글

mysql rank 구하기 (팀이 존재 하는 동일등수)  (0) 2017.01.16
mysql rank 구하기 (동일등수)  (1) 2017.01.13
mysql Rownum 구현하기  (0) 2017.01.12
Posted by 농부지기
,

** mysql 관리 Tool **

 

 

1. URL : http://bryan7.tistory.com/109

'(DB) MySql > 설치 및 초기DB작업' 카테고리의 다른 글

5. MySQL - TABLE 생성  (0) 2017.01.22
4. MySQL - DB 접속  (0) 2017.01.22
3. MySQL - DB 생성  (0) 2017.01.22
2. MySQL - Workbench 설치  (0) 2017.01.22
1. MySql 설치  (0) 2017.01.18
Posted by 농부지기
,

** Join 후 Update하기 **

 

1. 기본 예문

   UPDATE 테이블명 as A_table

          SET 업데이트할필드명  = (SELECT 선택필드

                                                     FROM 테이블명 as B_table

                                                   WHERE A_table.공통ID = B_table.공통ID);

2. Join후 Update구문

    UPDATE [테이블A] , [테이블B] b

          SET a.컬럼1 = b.컬럼1

     WHERE a.컬럼2 = b.컬럼2 ;

    

 

3. Join후 Update구문

    UPDATE 후원테이블 A INNER JOIN 회원테이블 B
                      ON A.회원아이디 = B.회원아이디
           SET B.회원등급 = 7
      WHERE B.회원등급 = 9 AND A.후원금 >= 10000 ;

 

 

4. mysql에서는 서브쿼리의 form절과 업데이트 target 모두를 같은 테이블로 사용할 수 없다
      update tbl_a
           set no = 'a'
         where seq in (
select * from ( select seq from tbl_a where x = 'b' ) as t )
     한번더 감싸준다.

 

 

 

Posted by 농부지기
,

** Rownum 구현하기**

 

1. sql

   SELECT @rnum := @rnum + 1

      FROM (SELECT @rnum := 0) rn

 

2. SELECT @rnum := 0  : from 절에  @rnum 이라는 변수 생성 및 초기화

    @rnum := @rnum + 1 : 컬럼 절에   @rnum 을 add한다.

 

 

3. set구문을 사용하여 rownum

    SET @rnum := 0;

 

    SELECT @rnum := @rnum + 1

         FROM 테이블명;

 

4. FROM절에서 초기화

    SELECT @rnum := @rnum + 1

       FROM (SELECT @rnum := 0) rn

 

5. WHERE절에서 초기화

    SELECT @rnum := @rnum + 1

       FROM 테이블명

    WHERE (@rnum := 0) = 0;    

Posted by 농부지기
,

** 문자열 결합 **

 

 

MySQL 함수중 GROUP_CONCAT과 CONCAT, CONCAT_WS 가 있다.
이 함수를 이용하면 우리가 원하는 결과를 얻을 수 있다.

1. CONCAT


CONCAT는 Field를 하나의 문자열로 묶어주는 함수이다.
다음과 같이 사용할 수 있다.

CONCAT 예제 (Language : sql)
SELECT CONCAT(`IDX`,'|',`NAME`,'|',`EMAIL`) FROM USER_TEST;

 

 


결과는 다음과 같다.

1|지도리|jidori@nnn.com
2|지돌스타|jidolstar@nnn.com
3|방실이|bangsiri@nnn.com
4|설운도|seolundo@nnn.com
5|박주영|juyoung@nnn.com
6|이효리|hyri@nnn.com


2. CONCAT_WS


CONCAT_WS는 CONCAT과 거의 비슷하다. 하지만 구분자를 한번에 지정할 수 있다는 것이 특징이다.

CONCAT_WS 예제 (Language : sql)
SELECT CONCAT_WS('|', `IDX``NAME``EMAIL`) FROM USER_TEST;



결과는 CONCAT과 완전 동일하다.

3. GROUP_CONCAT


이 함수를 이용하면 GROUPING된 Record를 원하는 구분자를 이용해 문자열로 통합할 수 있다.

예시는 다음과 같다.

GROUP_CONCAT 예제 (Language : sql)
SELECT GROUP_CONCAT(NAME SEPARATOR ';') FROM USER_TEST GROUP BY GROUPNUM;


결과는 다음과 같다.

지도리;지돌스타
방실이;설운도;이효리
박주영

단, 이것만 기억하자. group_concat_max_len 시스템 변수에 의해 최대허용길이를 설정할 수 있는데, default값이 1024이다. 만약 1024byte가 넘어가면 GROUP_CONCAT에 의해 붙은 문자열은 잘린다.

예문2) 결과 컬럼 약쪽에 '(single quote 넣기)

SELECT GROUP_CONCAT("'", part_cd, "'" ) AS part_cd_multi

  FROM hanjul.TPartition ;

결과 : '001', '002', '004' 




제대로 사용해보기 


Query문 부터 보자. 

가로행과 세로열을 모두 구분자로 묶기 (Language : sql)
SELECT  GROUP_CONCAT(
    CONCAT_WS('|'`IDX``NAME``EMAIL`) 
    SEPARATOR '@') 
FROM USER_TEST 
GROUP BY GROUPNUM;
 


결과는 다음과 같다. 

1|지도리|jidori@nnn.com;2|지돌스타|jidolstar@nnn.com
3|방실이|bangsiri@nnn.com;4|설운도|seolundo@nnn.com;6|이효리|hyri@nnn.com
5|박주영|juyoung@nnn.com

이 방법을 사용하면 한번의 Query로 처음에 보여준 PHP코드 처럼 할 필요가 없다는 것을 알 수 있을 것이다.

참고사이트

오라클에서 하는 방법 : http://blog.naver.com/xsoft/150017833358
GROUP_CONCAT() 메뉴얼 : http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
CONCAT() 및 CONCAT_WS() 메뉴얼 : http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

'(DB) MySql > String 함수' 카테고리의 다른 글

MySql - 문자열 변경. Replace  (0) 2017.01.18
MySql - 문자열 찾기.Find  (0) 2017.01.18
Posted by 농부지기
,

** Toast 모양 변경하기 **

 

0. 프로젝트명 : ToastModify

1. Toast컬러 변경 및 layout을 이용한 Toast변경

 

2. 소스

    1. MainActivity.xml              : layout 조회 및 Toast보여주기

    2. drawable/toast.xml        : Toast모양 변경

    3. layout/activity_main.xml : main activity

    4. layout/activity_toast.xml : Toast모양을 변경 할 Layout. Activity. View

 

3. 화면 :

    - Toast의 좌표

       .  메소드 : Toast.setGravity(int gravity, int xOffset, int yOffset)

       .  gravity : Toast의 기본 위치 지정 (TOP, LEFT, CENTER..)

       .  xOffset, yOffset : gravity 기본 위치를 기준으로 x, y 좌표임

       .  예)  toast.setGravity(Gravity.TOP | Gravity.LEFT150, 300);

 

    - [단순 토스트표시] 버튼 클릭 시 Toast

        

 

   - [LAYOUT토스트 표시]버튼  클릭 시 Toast

       . activity_toast.xml layout을 이용해서 생성

       . drawable/toast.xml 파일에 shape, strok, solid, padding, corner, gradient   tag등을 통해서 Toast 모양 변경

       


 

 

  1. MainActivity.xml

 

package com.example.farmer.toastmodify;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button;
    Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);

        button.setOnClickListener(toastOnclickListener);
        button2.setOnClickListener(layoutOnclickListener);
    }

    Button.OnClickListener toastOnclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Toast toast = Toast(getApplicationContext());
            //toast.setText(sResult);
            //toast.setDuration(Toast.LENGTH_LONG);

            String notes = "토스트 내용";

            Toast toast = Toast.makeText(getApplicationContext(), notes, Toast.LENGTH_SHORT);

            //Toast 위치 지정.
            //x,y 좌표는  TOP, LEFT 의 위치에서 시작되는 좌표임
            toast.setGravity(Gravity.TOP | Gravity.LEFT150, 300);

            //Toast는 View를 상속받지 않아서 직접View 객체에 대한 속성을 넣을 수 없다.
            //대신 getView()메소드를 통해서 View객체를 받아서 View속성정의 하면 Toast속성에 적용 된다.
            View viewToast = toast.getView();

            //Toast BackgroundColor 변경
            //color 표기시 xml 에서는 "#ffffff"로 하지만
            //java 코드에서는 아래와 같이 rgb로 변환하여 사용해줘야 한다.
            viewToast.setBackgroundColor(Color.rgb(255, 0, 255));

            //Toast Font Color변경
            TextView tvToast = (TextView) viewToast.findViewById(android.R.id.message);
            tvToast.setTextColor(Color.RED);

            toast.show();

            //Toast.makeText(getApplicationContext(), notes, Toast.LENGTH_LONG).show();
        }
    };

    Button.OnClickListener layoutOnclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //토스트 모양을 정의하고 있는 레이아웃을 인플레이션 한다.
            //기존 토스트 모양을 작성한 toastborder.xml로 인플레이션 한다.
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.activity_toast,
                    (ViewGroup)findViewById(R.id.toast_layout_root));

            //레이아웃의 텍스트뷰에 보여줄 문자열을 설정한다.
            TextView text = (TextView)layout.findViewById(R.id.text);
            text.setText("Layout을 이용한 토스트");

            //토스트 객체 만들기
            Toast toast = new Toast(MainActivity.this);

            //토스트가 출력될 위치를 지정
            //x,y 좌표는  CENTER 의 위치에서 시작되는 좌표임
            toast.setGravity(Gravity.CENTER, 0, -200);

            toast.setDuration(Toast.LENGTH_SHORT);

            //토스트에 뷰를 설정
            toast.setView(layout);

            //토스트 부여주기
            toast.show();
        }
    };
}

 

 

 

  2. drawable/toast.xml

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!--
      1. stroke    : 테두리 선
      2. solid      : Toast 내부 색 설정
      3. padding : 여백
      4. corners : 모서리 설정
      5. gradient : 경사도. 변화도
    -->
    <stroke
        android:width="4dp"
        android:color="#ff0008"
        android:dashWidth="5dp"
        android:dashGap="2dp"/>

    <solid
        android:color="#ff883300"/>

    <padding
        android:left="20dp"
        android:top="20dp"
        android:right="20dp"
        android:bottom="20dp"/>

    <corners
        android:radius="100dp"/>

    <gradient
        android:startColor="#ffff0000"
        android:endColor="#80ff00ff"
        android:angle="270"/>

</shape>

 

 

 

 

  3. layout/activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.farmer.toastmodify.MainActivity">

    <Button
        android:id="@+id/button"
        android:text="단순 토스트 표시"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:text="Layout 토스트 표시"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/button"
        android:layout_height="wrap_content" />

</RelativeLayout>

 

 

 

 

  4. layout/activity_toast.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="horizontal"
    android:background="#00ffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:background="@drawable/toast"/>

</LinearLayout>

 

 

 

Posted by 농부지기
,
[ log4j.xml   파일 예제 ]


'Web. 기타 언어 > log4j. slf4j' 카테고리의 다른 글

4. slf4j - 예제  (0) 2018.10.31
1. Log4j 구성 방법  (0) 2017.01.25
2. log4j 기본 설명  (0) 2017.01.12
Posted by 농부지기
,

프로그램작성시 로그를 남기는 목적으로 만든 프레임워크.

기본은 log4j.jar파일이 있고

확장파일로는 jdbcappender.jar와 ojdbc14.zip파일등이 있다.

 

log4j configuration 파일 설정하기

 

root looger설정

log4j.rootLogger=INFO(로그레벨), console, filelog, dblog(형식)

 

로그레벨을 설정하고 뒤에 root appender를 설정한다.  console(화면출력), filelog,dblog

log level은 debug,info,warn,error,fatal로 구성되고 debug제외 모든 로그가 기록된다.

① FATAL : 가장 크리티컬한 에러가 일어 났을 때 사용합니다. 하위

② ERROR : 일반 에러가 일어 났을 때 사용합니다.                    

③ WARN : 에러는 아니지만 주의할 필요가 있을 때 사용합니다. 1,2,3

④ INFO : 일반 정보를 나타낼 때 사용합니다.  1,2,3,4나타냄

⑤ DEBUG : 일반 정보를 상세히 나타낼 때 사용합니다. 최상위 모두1,2,3,4,5를 모두 나타낸다

 

 

그렇지만 예외적으로 선택적 패키지별로 로그를 사용할수 있다. 이것은

# package logging setting
log4j.logger.com.test(패키지명)=FATAL,logfile 이렇게 해주면 된다.

 

layout타입 : patternlayout

%p : debug,info 로그타입

%d : 로그발생시간 (%d{yyyy-MM-dd HH:mm:ss})

%t :로그 쓰레드 이름

%F: 로그 발생 프로그램 파일명(%F{파일명}

%M : 로그 발생메소드명

%r : 프로그램 시작후 로그 발생까지 시간

%m : 로그로 전달된 메세지

%n: 개행

%L 호출 코드 라인

 

참조한거다 다른 분이 올린거..

[%c] [%C] [%d] [%F] [%l] [%L] [%m] [%M] [%n] [%p] [%r] [%t] [%x] [%X]는 다음과 같다

admin.jsp] [org.apache.jsp.admin_jsp] [2011-02-10 11:30:22,21] ADMIN_jsp.java] [org.apache.jsp.test_jsp._jspService(test_jsp.java:64)] [64][fatal!!] [_jspService] [개행] [FATAL] [765567] [http-8080-Processor25] [] []

 

실제적용

먼저 web.xml에 적용

  <context-param>
     <param-name>log4jConfigLocation</param-name>
     <param-value>/WEB-INF/properties/log4j.properties</param-value>
    </context-param>

속성저장log4j.properties파일

 

## 세팅 stdout,rolling 두개의 appender add

log4j.rootLogger=DEBUG,stdout,fout 디폴트 로그 남기기 
## 콘솔에 출력하기위한  stdout생성
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
## Pattern
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# 호출자의 이름과 라인번호기록

log4j.appender.stdout.layout.ConversionPattern=[%d] %c (%F %L) -%m -%n
# roll 파일이름 설정


## 파일에 출력하기위한  fout생성

 

log4j.appender.fout=org.apache.log4j.DailyRollingFileAppender
## 이클립스의 경우에는  이클립스 자체경로C:\myjava\eclipse가 root가 되어버렸다.
log4j.appender.fout.File=logs/log4j.log\t
log4j.appender.fout.ImmediateFlush=true
log4j.appender.fout.Append=true\t
log4j.appender.fout.DatePattern='.'yyyy-MM-dd-HH(매시간마다)-mm(매분마다)
log4j.appender.fout.layout=org.apache.log4j.PatternLayout
log4j.appender.fout.layout.ConversionPattern=[%d] %c (%F %L) -%m -%n

 

실행방법

 

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;

 

   Logger log=Logger.getLogger(this.getClass());
   log.debug("자료설정"); info fatal등 속성에 따라 나온다.

이런식으로 넣어주면 알아서 로그파일이나 db에 생성해준다.


참조 : http://blog.naver.com/kimura777/110107337809

[출처] log4j|작성자 숲이면서 나무


'Web. 기타 언어 > log4j. slf4j' 카테고리의 다른 글

4. slf4j - 예제  (0) 2018.10.31
1. Log4j 구성 방법  (0) 2017.01.25
3. log4j.xml 파일 예제  (0) 2017.01.12
Posted by 농부지기
,