5.1 Dhaptar saringan

Logger ngidini sampeyan ngatur nyaring pesen kanthi apik. Kanggo nindakake iki, dheweke duwe sawetara rolas saringan kanthi macem-macem parameter. Tabel ing ngisor iki nuduhake sing paling populer.

Filter-filter Katrangan
1 BurstFilter Ngidini sampeyan ngontrol frekuensi pesen per detik kanggo level logging tartamtu.
2 Filter Komposit Ngidini sampeyan nyetel sawetara saringan urutan.
3 DynamicThresholdFilter Ngidini sampeyan ngaktifake logging rinci yen informasi tartamtu ditemokake ing log.
4 MapFilter Ngidini sampeyan mbangun ekspresi logis sing kompleks kanggo panyaring saka sawetara paramèter.
5 PenandaFilter Ngidini sampeyan nyaring pesen miturut tag, tag kasebut kudu ditambahake dhisik sajrone logging acara.
6 RegexFilter Ngidini sampeyan nyetel topeng - ekspresi biasa.
7 StructuredDataFilter Ngidini sampeyan nyaring pesen kanthi anané data tartamtu.
8 ThreadContextMapFilter Ngidini sampeyan ngatur saringan adhedhasar data sing dijupuk saka konteks utas saiki.
9 ThresholdFilter Kontrol logging adhedhasar level pesen log.
10 WektuFilter Ngidini sampeyan ngaktifake lan mateni saringan ing wektu tartamtu.

Ing ngisor iki kita bakal ngomong babagan mung telu. Sampeyan bisa sinau luwih lengkap babagan saringan kasebut ing situs web resmi .

5.2 Filter Wektu

Filter kasebut TimeFilterngidini sampeyan ngaktifake lan mateni saringan ing wektu tartamtu, ana 5 parameter:

1 miwiti Wektu wiwitan logging ing formatHH:mm:ss
2 pungkasan Wektu log off ing formatHH:mm:ss
3 zona wektu Nyetel zona wektu.
4 onMatch Carane log yen kondisi filter bener . Mungkin ACCEPT, DENYutawa NEUTRAL. Default NEUTRAL.
5 ing Mismatch Carane mlebu log yen kondisi filter salah . Mungkin ACCEPT, DENYutawa NEUTRAL. Default NEUTRAL.

Ayo nyiyapake saringan sing bakal nguripake logging jam 5 esuk lan mateni jam 5:30 saben dina. Conto kanthi konfigurasi filter lengkap ing ngisor iki:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
    <TimeFilter start="05:00:00" end="05:30:00" onMatch="ACCEPT" onMismatch="DENY"/>
    <PatternLayout>
        <pattern>%d %p %c{1.} [%t] %m%n</pattern>
    </PatternLayout>
    <TimeBasedTriggeringPolicy />
    </RollingFile>
</Appenders>
<Loggers>
    <Root level="error">
    <AppenderRef ref="RollingFile"/>
    </Root>
</Loggers>
</Configuration>

5.3 RegexFilter

Filter RegexFilterngidini sampeyan nyetel topeng (ekspresi reguler) kanggo nyaring pesen. Filter iki duwe 4 parameter:

1 regex Nemtokake ekspresi reguler - topeng sing digunakake kanggo nyaring pesen.HH:mm:ss
2 nggunakakeRawMsg Topeng ditrapake ing pesen sadurunge format (bener) utawa sawise format (salah)
3 onMatch Carane log yen kondisi filter bener . Mungkin ACCEPT, DENYutawa NEUTRAL. Default NEUTRAL.
4 ing Mismatch Carane mlebu log yen kondisi filter salah . Mungkin ACCEPT, DENYutawa NEUTRAL. Default NEUTRAL.

Ayo nyiyapake saringan sing mung ngidini pesen sing ngemot tembung codegym. Conto kanthi konfigurasi filter lengkap ing ngisor iki:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log"   filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
    <RegexFilter regex=".* codegym .*" onMatch="ACCEPT" onMismatch="DENY"/>
    <PatternLayout>
        <pattern>%d %p %c{1.} [%t] %m%n</pattern>
    </PatternLayout>
    <TimeBasedTriggeringPolicy />
    </RollingFile>
  </Appenders>
<Loggers>
    <Root level="error">
    <AppenderRef ref="RollingFile"/>
    </Root>
</Loggers>
</Configuration>

5.4 Ambang Filter

Filter ThresholdFilter ngidini sampeyan ngatur nyaring miturut level pesen. Ana mung 3 parameter:

1 tingkat Nyetel jeneng level logging pesen: ERROR, DEBUG,…
2 onMatch Carane log yen kondisi filter bener . Mungkin ACCEPT, DENYutawa NEUTRAL. Default NEUTRAL.
3 ing Mismatch Carane mlebu log yen kondisi filter salah . Mungkin ACCEPT, DENYutawa NEUTRAL. Default NEUTRAL.

Ayo nyiyapake saringan sing mung ngidini pesen level DEBUG. Conto kanthi konfigurasi filter lengkap ing ngisor iki:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
       <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
    <PatternLayout>
        <pattern>%d %p %c{1.} [%t] %m%n</pattern>
    </PatternLayout>
    <TimeBasedTriggeringPolicy />
    </RollingFile>
</Appenders>
<Loggers>
    <Root level="error">
    <AppenderRef ref="RollingFile"/>
    </Root>
</Loggers>
</Configuration>