BurningBright

  • Home

  • Tags

  • Categories

  • Archives

  • Search

Determinant(2)

Posted on 2017-12-24 | Edited on 2020-06-16 | In math

Exchange

定理一 一个排列的任意两个元素对换,排列改变奇偶性

  • 对相邻元素对换有:

    • 如果 a > b,则a的逆序数不变,b的逆序数-1
    • 如果 a < b,则a的逆序数+1,b的逆序数不变
      得到相邻元素兑换排列改变奇偶性
  • 基于以上相邻元素对一般情况对换有:

    • 将b对换到a后需要n次对换
    • 将a对换到后需要n+1次对换对换a b对应2n+1次奇偶性改变,排列的奇偶性改变

推论 奇排列变成标准排列,对换次数是奇数次;偶排列变成标准排列,对换次数是偶数次

对行列式的排列做一次对换,其中 1 2 … i … j … n 是自然顺序,
p1 p2 … pi … pj …pn 是列的某个排列。对换后有:

其中 1 2 … j … i … n 的逆序数的奇数r,p1 p2 … pj … pi …pn 的逆序数将反转设其逆序数为t1

同时改变行号列号不改变奇偶性,即对调行列式排列元素不会改变符号。

由此可得出存唯一行序列q1 q2 … qn,使得列序列标准化:

定理二 n阶行列式也可定义为

Property

性质1 行列式和它的转置行列式相等

所有元素行列互换,并不影响行列式的值

性质2 互换行列式的两行(列),行列式变号

换行后的行列式表示为

互换的行(列)排列变奇偶性,求和后即整体变号

推论 如果有两行(列)完全相同,行列式为0

互换两行(列)后有 D = -D1, 行列式为0

性质3 行列式某行(列)都乘以k,等于k乘以该行列式

记作
提出公因子后记作

性质4 行列式中如果有两行(列)成比例,行列式为等于零

根据性质3提出公因子后,两行(列)相同。根据性质2,行列式为0

性质5 如果行列式某行(列)都是两数的和,则可分为两行列式的和

假设组合的第i行元素都可分两数之和:

性质6 把行列式某行(列)的元素乘以同一数,加到另一行(列),行列式不变

根据性质4 和 性质5,将加上的等比例数拆分出另一个行列式,值为0,即不改变行列式

Determinant (1)

Posted on 2017-12-22 | Edited on 2020-06-16 | In math

Primary

消去后当时得到:

按方程组中的顺序排好行列:

分母即是行列式的主对角线”积” - 副对角线”积”
如果定义:

则有:

example

n Factorial determinant

In 3 factorial determinant:

带负号系数的组合都是逆序数为奇数的组合,用p1p2p3来代表某组合矩阵位置有

t表示p1p2p3组合时的逆序数,取和即使为矩阵的表示

More generalizate

简记作$det(a{ij})$, 数$a{ij}$为D的(i, j)元

Prove

符号代表着副对角线的元素,对$\lambdai$可以用$a{i, n-i+1}$来表示。

对n n-1 n-2 … 1 组合的元素求逆序数

Abbreviations and symbols

Posted on 2017-12-21 | Edited on 2020-06-16 | In ml

缩写

a b c
AR autoregressive 自回归
BBTT back propagation through time 通过时间的反向传播
BM Boltzmann machine Boltzmann机
BP back propagation 反向传播
b/s bit per second 每秒比特率
BSB bran-state-in-a-box 盒中脑状态
BSS blind source(signal) separation 盲源(信号)分离
cmm correlation matrix memory 相关矩阵记忆
CV cross-validation 交叉验证
DFA deterministic finite-state automata 确定性有限状态自动机
EKF extended Kalman filter 扩展卡尔曼滤波器
EM expectation-maximization 期望最大化
FIR finite-duration impulse response 有限时间冲击响应
FM frequency-modulated(singal) 频率调制(信号)
GCV generalized cross-validation 广义交叉验证
GHA generalized Hebbian algorithm 广义Hebb算法
GSLC generalized sodelobe canceler 广义旁瓣消除器
Hz hertz 赫兹
ICA 独立分量分析
Infomax 最大互信息
Imax 最大互信息的变体
Imin 最大互信息的另一变体
KSOM 核自组织映射
KHA 核Hebb算法
LMS least-mean-square 最小均方
LR 似然比
LS Least-squares 最小二乘
LS-TD Least-squares, temporal-difference 最小二乘,时序差分
LTP 长期增强
LTD 长期衰减
LRT 似然比测试
MAP 最大后验估计
MCA 次分量分析
MCMC Markov Chain Monte Carlo 马尔可夫链蒙特卡洛
MDL 最小描述长度
MIMO 多输入多输出
ML 最大似然
MLP Multilayer perceptron 多层感知器
MRC 模型参考控制
NARMA 非线性自回归平滑平均
NARX 具有外部输入的非线性自回归
NDP 神经动态规划
NW Nadaraya-Watson(估计器)
NWKR Nadaraya-Watson核回归
OBD 最优脑损伤
OBS 最优脑外科
OCR optical character recognition 光学字符识别
PAC 可能近似正确
PCA 主分量分析
PF 粒子滤波器
pdf probability density function 概率密度函数
pmf probability mass function 概率质量函数
QP
RBF
RLS
RLS
RMLP
RTRL
SIMO
SIR
SIS
SISO
SNR
SOM
SRN
SVD
SVM
TD
TDNN
TLFN
VC
VLSI
XOR exclusive OR 异或

符号

a b c
$\mathrm{a}$ action 动作
$\mathbf{a}^T\mathbf{b}$ 向量a和b的内积
$\mathbf{ab}^T$ 向量a和b的外积
$\dbinom{l}{m}$ 二项式系数
$\mathrm{A}\bigcup\mathrm{B}$ A和B的并集
$\mathrm{B}$ 温度的逆
$\mathrm{b}_k$ 神经元k的偏置
$\cos(\mathbf{a},\mathbf{b})$ 向量a的b夹角余弦
$\mathit{c}_{ u, v } (\mathit{u, v})$ 系词的概率密度函数
$\mathit{D}$ 记忆深度
$\mathit{D}_{ f | g }$ 概率密度函数f和g之间的Kullback-Leibler散度
$\tilde{\mathbf{D}}$ 算子D的伴随矩阵
$\mathit{E}$ 能量函数
$\mathit{E}_i$ 统计力学中的状态i的能量
$\mathbb{E}$ 统计期望算子
$\left\langle \mathit{E} \right\rangle$ 平均能量
$\exp$ 指数
$\varepsilon_{av}$ 平均平方误差或平方误差和
$\varepsilon(n)$ 平方误差和的瞬间值
$\varepsilon_{total}$ 总平方误差和
$\mathit{F}$ 自由能量
$\mathscr{F}*$ 经验风险最小的子集(网络)
$\mathbf{H}$ Hessian矩阵
$\mathbf{H}^{-1}$ Hessian矩阵H的逆
$\mathit{i}$ -1的平方根,亦记作j
$\mathbf{I}$ 单位矩阵
$\mathbf{I}$ Fisher信息矩阵
$\mathit{J}$ 均方误差
$\mathbf{J}$ Jacobi矩阵
$\mathbf{P}^{1/2}$ 矩阵P的方根
$\mathbf{P}^{T/2}$ 矩阵P的方根转置
$\mathbf{P}_{n, n-1}$ 卡尔曼滤波理论中的误差协方差矩阵
$\mathit{k}_B$ Boltzmann常数
$\log$ 对数
$\mathit{L}(\mathbf{w})$ 权值向量w的对数似然函数
$\mathscr{L}(\mathbf{w})$ 但样本的权值向量w的对数似然函数
$\mathbf{M}_c$ 可控矩阵
$\mathbf{M}_o$ 可观察矩阵
$\mathrm{n}$ 离散时间
$\mathit{P}_i$ 统计力学中的状态i的概率
$\mathit{P}_{ij}$ 从状态i到状态j的转移概率
$\mathbf{P}$ 随机矩阵
$\mathrm{P}(e|\mathscr{C})$ 从类C中输入时e的条件概率
$\mathrm{P}_\alpha^+$ 假设网络处于钳制状态(正向阶段)时,Blotzmann机的可见神经元状态为α的概率
$\mathrm{P}_\alpha^-$ 假设网络处自有运行条件(负向阶段)时,Blotzmann机的可见神经元状态为α的概率
$\hat{r}_x(j, k; n)$ xi(n)和xk(n)的自相关函数估计
$\hat{r}_{dx}(k; n)$ d(n)和xk(n)的交叉相关函数估计
$\mathbf{R}$ 输入向量的相关矩阵
$\mathrm{t}$ 连续时间
$\mathit{T}$ 温度
$\mathscr{T}$ 训练集(样本)
$\mathrm{tr}$ 表示矩阵迹的算子
$\mathrm{var}$ 方差算子
$\mathit{V}(x)$ 状态向量x的Lyapunov函数
$\mathit{v}_i$ 神经元j的诱导局部域或激活位势
$\mathbf{w}_v$ 突出权值向量的最优值
$\mathscr{w}_{kj}$ 属于神经元k的突触j的突触权值
$\mathbf{w}^*$ 最优向量x的平衡值
$\bar{\mathbf{x}}$ 状态向量x的平衡值
$\left\langle \mathit{x}_i \right\rangle$ “热”意义下状态xj的平均
$\hat{\mathit{x}}$ x的估计,用加字符号^表示
$|\mathit{x}|$ x的绝对值(幅度)
$\mathit{x}^*$ 状态x的复共轭,用*作上标
$||\mathbf{x}||$ 向量x的欧几里得范数
$\mathbf{x}^T$ 向量x的转置,用上标T表示
$\mathit{z}^{-1}$ 单位时间延迟算子
$\mathrm{Z}$ 部分函数
$\delta_j(n)$ 神经元j在时刻n的局部梯度
$\Delta\mathit{w}$
$\nabla$ 梯度算子
$\nabla^2$ 拉普拉斯算子
$\nabla_w\mathit{J}$ J关于w的梯度
$\nabla \centerdot \mathbf{F}$ 向量F的散度
$\eta$ 学习率参数
$\kappa$ 累积量
$\mu$ 策略
$\theta_k$ 神经元k的阀值(偏置bk的负值)
$\lambda$ 正则化参数
$\lambda_k$ 方阵的第k个特征值
$\varphi_k(\centerdot)$ 神经元k的非线性激活函数
$\in$ “属于”符号
$\bigcup$ “并”符号
$\bigcap$ “交”符号
$*$ 卷积符号
$+$ 矩阵伪逆的上标符号
$+$ 更新估计的上标符号

Thread security

Posted on 2017-12-02 | Edited on 2018-12-16 | In java

Security problem in java web

In tradition web develop, Servlet implement response to http request.
Servlet is one of the important standard of J2EE, it set how java response to Http request.
In project, classpath need relay on servlet-api.jar of j2ee.jar.
We can interact with web container through HttpServletRequest and HttpServletResponse object.

When container receive a request, container will dispatch a thread that
define in advance from main thread(tomcat observer pattern), to deal the mapping servlet implement.
When another request come in, main thread will choose thread from threadpool to serve,
but it do not care is there other threads using this implement.

So web container use singleton pattern to implement service object.
This pattern can reduce the servlet space waste, and improve response performance.
But it lead to security problem, the servlet in container is not thread security save.

HashMap and HashTable

  • HashMap implement Map interface from java 1.2
  • a light weight implement of HashTable, no contains method(avoid confusion)
  • All key / value in HaspMap entry are object(can be null).
  • not security save

  • HashTable is security save, it’s method is Synchronized

  • HashTable & HashMap hash implement is same, so performance in Synchronized is same too.

Theory

if multi-thread program run in same code result is same with single thread expect,
then thread secure. Thread security is caused by global or static changeable variable.
If only read operation on these variable in program, then no thread security problem in regular.

Thread security

In multi-thread environment, a class execute method, obtain independent resource

  • method signature variable is save(???)
  • local variable in method is save

ThreadLocal

ThreadLocal is self is not a thread, it’s static class.
It maintain thread’s independent copy ThreadLocalMap.

one side: all thread visit it’s own ThreadLocalMap
another side: one thread visit it’s own ThreadLocal

Summary

  • ThreadLocal maintain thread’s variable, to deal visit conflict.
  • Synchronized use JVM lock mechanism to make sure resource atomicity(one thread visit in time).

ThreadLocal use space to exchange time, provide every thread a variable copy, make sure visit concurrent but no conflict.
Synchronized use time to exchange space, monitor a target, to make thread visit in queue.

http://blog.51cto.com/dongisland/1369375

sleep in c++

Posted on 2017-11-15 | Edited on 2018-12-16 | In c++

Introduce

hang up thread for a while
unsigned sleep(unsigned seconds);

  • In VC++ add head file #include <windows.h> or #include "stdafx.h"
  • In GCC add head file #include <unistd.h>

Caution

  • In windows Sleep() is first letter upper case, and unit in milliseconds
  • In Linux sleep() is first letter lower case, and unit in seconds

</br>
</br>
</br>
</br>
</br>
</br>
</br>

List

a b
Dos sleep(1); //停留1秒 </br>delay(100); //停留100毫秒
Windows Sleep(100); //停留100毫秒
Linux sleep(1); //停留1秒 </br>usleep(1000); //停留1毫秒

sleep function

Think in Develop

Posted on 2017-10-27 | Edited on 2018-12-16 | In theory

Split front and backgroud

Now adays the front develop framwork has a great progress,
more and more web develop use model/ scriptable develop framwork.

Backgroud no need forcus on how to deal page staff, much more backgroud.
More and more POST style backgroud, restructure to REST style.

Front emphasize in rendering

App’s rendering got a great progress, thanks to mobile equipment popular.
Front developer now have more power to deal business with customer,
no need to care how hard backgroud gay work.

Backgroud emphasize in usability

Backgroud’s structure is important, it’s stability/ usability affect
user feeling a lot.

Backgroud developer more forcus on data/ model/ resource/ performan…

Develop now

For front developer, MVC is relatively balance.
For backgroud developer, V is steal useable in some old style develop
project, use script style backgroud language PHP JSP ASP …

But for robust modern business application, B/G have been splitted

https://segmentfault.com/a/1190000004213733

Linux memory

Posted on 2017-10-16 | Edited on 2018-12-16 | In linux
  1. get top memory process

    1
    2
    # +3[CPU]
    ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head
  2. free

    1
    2
    3
    4
    5
    6
    7
    8
    free -m
    free -b -s5
    free -h

    total used free shared buffers cached
    Mem: 7.7G 7.6G 133M 164K 190M 3.1G
    -/+ buffers/cache: 4.2G 3.5G
    Swap: 0B 0B 0B

from the point of view of OS buffers and cached memory is used
available memory size = free + buffers + cached

  1. ps
    1
    2
    ps aux --sort -rss
    ps aux | grep xxx
  • USER process’ user
  • PID process id
  • %CPU cpu proportion
  • %MEM memory proportion
  • VSZ total VM size in kB
  • RSS resident set size
  • TTY terminal
  • STAT
    • R Running or runnable (on run queue)
    • S Interruptible sleep (waiting for an event to complete)
  • START time the command started
  • TIME run cpu time
  • COMMAND string as arguments
  1. /proc
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    cat /proc/pid/status

    VmPeak: 5033936 kB
    VmSize: 5033620 kB
    VmLck: 0 kB
    VmHWM: 1151928 kB
    VmRSS: 1151644 kB
    VmData: 4875980 kB
    VmStk: 88 kB
    VmExe: 36 kB
    VmLib: 127412 kB
    VmPTE: 2724 kB
    VmSwap: 0 kB
    Threads: 72
  • VmSize(KB) task’s virtual address space size (total_vm-reserved_vm)
  • VmLck(KB) been locked physical memory size, can’t exchange to hard drive(locked_vm)
  • VmRSS(KB) resident set size, physical memory. (rss in ps)
  • VmData(KB) program’s data paragraph size (virtual), store initialized data(total_vm-shared_vm-stack_vm)
  • VmStk(KB) task’s stack size in user(stack_vm)
  • VmExe(KB) executable virtual memory size, (exclude lib) (end_code-start_code)
  • VmLib(KB) lib mapping to task memory space size(exec_lib)
  • VmPTE(KB) this process’s page size
  • Threads share this signal’s task number(POSIX multi app, thread group use same signal)
  1. top
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # [shit+m sort by memory]
    top -p 6506

    top - 11:14:20 up 125 days, 1:47, 1 user, load average: 0.00, 0.00, 0.00
    Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
    Cpu(s): 0.2%us, 0.1%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
    Mem: 8061396k total, 7925220k used, 136176k free, 194748k buffers
    Swap: 0k total, 0k used, 0k free, 3291676k cached

    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    3825 root 20 0 164m 41m 1020 S 0.0 0.5 146:54.42 redis-server

Cpu(s): 0.2%us, 0.1%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st

  • us user space cup proportion
  • sy system core space cup proportion
  • ni priority changed task cup proportion
  • id idle cpu proportion
  • wa waiting IO cpu proportion
  • hi hard ware cost
  • si soft ware cost
  • st steal time

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

  • PR priority rank
  • NI nice value
  • VIRT virtual memory size
  • RES not be exchanged memory size
  • SHR share meory size

find command

Posted on 2017-09-27 | Edited on 2018-12-16 | In linux

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression]

find [path path …]

Option

  • -depth step into sub files first, then current directory
    .The -delete action also implies -depth

  • -name [-iname] find file by name
    find /etc /root -name passwd

  • -type

    • d directory
    • f regular file
  • -size

    • b for 512-byte blocks (this is the default if no suffix is used)
    • c for bytes
    • w for two-byte words
    • k for Kilobytes (units of 1024 bytes)
    • M for Megabytes (units of 1048576 bytes)
    • G for Gigabytes (units of 1073741824 bytes)
    1
    2
    3
    4
    # find files bigger than 30 kilobytes
    # %k %b in printf represent different block size

    find -size +30k -printf '%p %k %b \r\n'
  • -a[and] -o[or] -not logic option

    1
    2
    3
    4
    5
    6
    # find files name end with sh, and belong to user
    # ubuntu or group id is 1000
    find /usr -name '*.sh' -a -user ubuntu -o -gid 1000

    # find files name not end with class
    find . -not -name '*.class'
  • -atime -mtime -ctime -amin -mmin -cmin
    a access file
    m modify file
    c change property
    time days
    min minutes

    1
    2
    3
    4
    5
    # find files be accessed in last 19 days
    find -atime -19 -printf '%p %a %t \r\n'

    # find files not be accessed in last 19 days
    find -atime +19 -printf '%p %a %t \r\n'
  • -newer -anewer -cnewer
    based on parameter file modify time
    -newer File was modified more recently than file
    -anewer File was last accessed more recently than file was modified.
    -cnewer File’s status was last changed more recently than file was modified.

  • -perm
    find files based on privilege

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # find files in current directory which
    # permissions is -rwxr-xr-x
    find -perm 755

    # at least a kind of user have write privilege
    find -perm +444

    # all kind user have write privilege
    find -perm -444

Action

  • -printf format out put

    1
    2
    3
    #   name  depth  space[512-kbytes]  space[1024-kbytes]
    # permissions access_time change_time modify_time
    find -user ubuntu -printf '%p %d %b %k %M %a %c %t \r\n'
  • -ls
    list in ls command

  • -ok
    ask user yes/no execute command

  • -exec command {} +
    execute command no asking

    1
    2
    3
    4
    # delete files in current directory no accessed in 30 days
    find . -atime +30 –exec rm –rf {}
    # same effect
    find . -atime +30 | xargs rm -rf

linux find

Tomcat Container

Posted on 2017-09-20 | Edited on 2018-12-16 | In java

Tomcat container is a typical case of Chain of Responsibility pattern
Container composed by Engine Host Context Wrapper

connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);

  1. every container have it’s own pip and valve
  2. getPipeline() got pipline determine data direction
  3. getFirst() got valve determine door open or close
  4. Interface Valve have method invoke

Engine

engine is top level container, it dispatch request to host

Host

a host represent a virtual machine like ‘localhost’, it store host reference info.

Context

context represent a web project in webapps

Wrapper

wrapper response for a servlet, like load/ init/ resource recycle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:662)

log4j configuration

Posted on 2017-09-18 | Edited on 2020-09-17 | In java

level

log4j.rootLogger = [level],appenderName,appenderName2,…

level show log’s privilege, OFF,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,ALL
log4j recommand 4 level, from low to high : DEBUG, INFO, WARN, ERROR

output

appenderName point logs’ output target
log4j.appender.xxx
log4j.appender.T.xxx

appender have these

  1. org.apache.log4j.ConsoleAppender
    to console
  2. org.apache.log4j.FileAppender
    to file
  3. org.apache.log4j.DailyRollingFileAppender
    create a file everyday
  4. org.apache.log4j.RollingFileAppender
    to some size make a new file
  5. org.apache.log4j.WriterAppender
    stream to everywhere

ConsoleAppender

  • -Threshold = DEBUG:log lowest output level
  • -ImmediateFlush = TRUE:default is true, all log will output immediately
  • -Target = System.err:default is System.out, out put to console(err is red,out is black)

FileAppender

  • -Threshold = INFO:log lowest output level
  • -ImmediateFlush = TRUE:default is true, all log will output immediately
  • -File = log/log4j.log:output file path
  • -Append = FALSE:default true, add message to file, false override file
  • -Encoding = UTF-8:appoint file encoding

DailyRollingFileAppender

  • -Threshold = WARN:log lowest output level
  • -ImmediateFlush = TRUE:default is true, all log will output immediately
  • -File = log/log4j.log:output file path
  • -Append = FALSE:default true, add message to file, false override file
  • -DatePattern=’.’yyyy-ww:every week
  • ‘.’yyyy-MM:month
  • ‘.’yyyy-ww:week
  • ‘.’yyyy-MM-dd:day
  • ‘.’yyyy-MM-dd-a:twice a day
  • ‘.’yyyy-MM-dd-HH:hour
  • ‘.’yyyy-MM-dd-HH-mm:minute
  • -Encoding = UTF-8:appoint file encoding

RollingFileAppender

  • -Threshold = ERROR:log lowest output level
  • -ImmediateFlush = TRUE:default is true, all log will output immediately
  • -File = log/log4j.log:output file path
  • -Append = FALSE:default true, add message to file, false override file
  • -MaxFileSize = 100KB:suffix can be KB MB GB when file met the threshold,
    new file will be created like log4j.log.1 log4j.log.2
  • -MaxBackupIndex = 2:appoint rolling files’ up limit
  • -Encoding = UTF-8:appoint file encoding

layout

  1. org.apache.log4j.HTMLLayout
  2. org.apache.log4j.PatternLayout
    customized layout
  3. org.apache.log4j.SimpleLayout
    contain log level, string so on
  4. org.apache.log4j.TTCCLayout
    contain time/ thread/ class info so on
  5. org.apache.log4j.xml.XMLLayout

HTMLLayout

  • -LocationInfo = TRUE:default false, output java file and row num
  • -Title=Struts Log Message:默认值 Log4J Log Messages

PatternLayout

  • -ConversionPattern = %m%n:formatting message [reference to Format]

XMLLayout

  • -LocationInfo = TRUE:default false, output java file and row num

Format

similar with c style
| a | b |
| —- | —- |
| %m | message in code |
| %p | level:DEBUG,INFO,WARN,ERROR,FATAL |
| %r | Millisecond from code to file |
| %c | class full name |
| %t | Thread |
| %n | Windows \r\n Unix \n Max \r |
| %d | ISO8601 in default or %d{yyyy-MM-dd HH\:mm\:ss,SSS} like: 2017-09-18 22:10:28,921 |
| %l | detail in event like: Testlog.main(TestLog.java:10)|
| %F | file name |
| %L | code row num |
| %x | 输出和当前线程相关联的NDC(嵌套诊断环境),像java servlets多客户多线程的应用中 |
| %% | “%” |
| %5c | min width right alignment in default |
| %-5c | min width - left alignment |
| %.5c | max width split left char |
| %20.30c | less than 20 add blank, more than 30 split from left |

Demo

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
#OFF,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB,ALL
log4j.rootLogger =ALL,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB

#输出到控制台
log4j.appender.systemOut = org.apache.log4j.ConsoleAppender
log4j.appender.systemOut.layout = org.apache.log4j.PatternLayout
log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
#log4j.appender.systemOut.layout.ConversionPattern = %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\: %m%n
#log4j.appender.systemOut.layout.ConversionPattern = %d{yyyy-MM-dd HH\:mm\:ss,SSS} %5p [%t] (%F\:%L) - %m%n
log4j.appender.systemOut.Threshold = DEBUG
log4j.appender.systemOut.ImmediateFlush = TRUE
log4j.appender.systemOut.Target = System.out

#输出到文件
log4j.appender.logFile = org.apache.log4j.FileAppender
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logFile.Threshold = DEBUG
log4j.appender.logFile.ImmediateFlush = TRUE
log4j.appender.logFile.Append = TRUE
log4j.appender.logFile.File = ../Struts2/WebRoot/log/File/log4j_Struts.log
log4j.appender.logFile.Encoding = UTF-8

#按DatePattern输出到文件
log4j.appender.logDailyFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logDailyFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logDailyFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logDailyFile.Threshold = DEBUG
log4j.appender.logDailyFile.ImmediateFlush = TRUE
log4j.appender.logDailyFile.Append = TRUE
log4j.appender.logDailyFile.File = ../Struts2/WebRoot/log/DailyFile/log4j_Struts
log4j.appender.logDailyFile.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.logDailyFile.Encoding = UTF-8

#设定文件大小输出到文件
log4j.appender.logRollingFile = org.apache.log4j.RollingFileAppender
log4j.appender.logRollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logRollingFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logRollingFile.Threshold = DEBUG
log4j.appender.logRollingFile.ImmediateFlush = TRUE
log4j.appender.logRollingFile.Append = TRUE
log4j.appender.logRollingFile.File = ../Struts2/WebRoot/log/RollingFile/log4j_Struts.log
log4j.appender.logRollingFile.MaxFileSize = 1MB
log4j.appender.logRollingFile.MaxBackupIndex = 10
log4j.appender.logRollingFile.Encoding = UTF-8

#用Email发送日志
log4j.appender.logMail = org.apache.log4j.net.SMTPAppender
log4j.appender.logMail.layout = org.apache.log4j.HTMLLayout
log4j.appender.logMail.layout.LocationInfo = TRUE
log4j.appender.logMail.layout.Title = Struts2 Mail LogFile
log4j.appender.logMail.Threshold = DEBUG
log4j.appender.logMail.SMTPDebug = FALSE
log4j.appender.logMail.SMTPHost = SMTP.163.com
log4j.appender.logMail.From = xxx@163.com
log4j.appender.logMail.To = xxx@gmail.com
#log4j.appender.logMail.Cc = xxx@gmail.com
#log4j.appender.logMail.Bcc = xxx@gmail.com
log4j.appender.logMail.SMTPUsername = leon
log4j.appender.logMail.SMTPPassword = 123456
log4j.appender.logMail.Subject = Log4j Log Messages
#log4j.appender.logMail.BufferSize = 1024
#log4j.appender.logMail.SMTPAuth = TRUE

#将日志登录到MySQL数据库
log4j.appender.logDB = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.logDB.layout = org.apache.log4j.PatternLayout
log4j.appender.logDB.Driver = com.mysql.jdbc.Driver
log4j.appender.logDB.URL = jdbc:mysql://127.0.0.1:3306/xxx
log4j.appender.logDB.User = root
log4j.appender.logDB.Password = 123456
log4j.appender.logDB.Sql = INSERT INTOT_log4j(project_name,create_date,level,category,file_name,thread_name,line,all_category,message)values('Struts2','%d{yyyy-MM-ddHH:mm:ss}','%p','%c','%F','%t','%L','%l','%m')

log4j.properties

1…151617…29

Leon

282 posts
20 categories
58 tags
GitHub
Links
  • clock
  • typing-cn
  • mathjax
  • katex
  • cron
  • dos
  • keyboard
  • regex
  • sql
  • toy
© 2017 – 2024 Leon
Powered by Hexo v3.9.0
|
Theme – NexT.Muse v7.1.2