0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

log4j2でログファイルの権限設定

Last updated at Posted at 2018-09-06

log4j2で出力されるログファイル、デフォルトだと所有者にだけ書き込み権限がついていた。別ユーザでファイルを触る必要があり、グループにも権限が欲しい。
やれやれ。僕は検索した。

Log4j 2 Appenders

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" name="MyApp" packages="">
  <Properties>
    <Property name="baseDir">logs</Property>
  </Properties>
  <Appenders>
    <RollingFile name="RollingFile" fileName="${baseDir}/app.log"
          		 filePattern="${baseDir}/$${date:yyyy-MM}/app-%d{yyyyMMdd}.log.gz"
                 filePermissions="rw-------">
      <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
      <CronTriggeringPolicy schedule="0 0 0 * * ?"/>
      <DefaultRolloverStrategy stopCustomActionsOnError="true">
        <PosixViewAttribute basePath="${baseDir}/$${date:yyyy-MM}" filePermissions="r--r--r--">
        	<IfFileName glob="*.gz" />
        </PosixViewAttribute>
      </DefaultRolloverStrategy>
    </RollingFile>
  </Appenders>
 
  <Loggers>
    <Root level="error">
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
 
</Configuration>

これによるとRollingFileの属性としてfilePermissionsを書いてあげればいいらしい。

File permissions log4j2

↑によると、どうも2.9.0以降の機能とのこと
残念ながら2.7.0を使っていたのでgradleで最新版を落とした

ext {
    log4jVersion = '2.11.1'
}
dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4jVersion}"
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4jVersion}"
}

使っていたlog4j2.xmlの定義に合わせるとこんな感じ

<RollingFile name="logfile"
             append="true"
             fileName="path/to/log" 
             filePermissions="rw-rw-r--">
  <PatternLayout pattern="${pattern.of.log}"/>
  <Policies>
    <TimeBasedTriggeringPolicy />
  </Policies>
</RollingFile>

これでよし。次にxmlが読み込まれるタイミングで対象のファイル権限が変更された。
当然といえば当然だけど、既にローテートされた過去のファイルに関しては権限はそのままだった。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?