<commit_before>/*
 * Copyright 2010 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twitter.logging

import com.twitter.util.{HandleSignal, StorageUnit, Time}
import java.io.{File, FilenameFilter, FileOutputStream, OutputStream}
import java.nio.charset.Charset
import java.text.SimpleDateFormat
import java.util.{Calendar, Date, logging => javalog}

sealed abstract class Policy
object Policy {
  case object Never extends Policy
  case object Hourly extends Policy
  case object Daily extends Policy
  case class Weekly(dayOfWeek: Int) extends Policy
  case object SigHup extends Policy
  case class MaxSize(size: StorageUnit) extends Policy
}

object FileHandler {
  val UTF8 = Charset.forName("UTF-8")

  /**
   * Generates a HandlerFactory that returns a FileHandler
   *
   * @param filename
   * Filename to log to.
   *
   * @param rollPolicy
   * When to roll the logfile.
   *
   * @param append
   * Append to an existing logfile, or truncate it?
   *
   * @param rotateCount
   * How many rotated logfiles to keep around, maximum. -1 means to keep them all.
   */
  def apply(
    filename: String,
    rollPolicy: Policy = Policy.Never,
    append: Boolean = false,
    rotateCount: Int = -1,
    formatter: Formatter = new Formatter(),
    level: Option[Level] = None
  ) = () => new FileHandler(filename, rollPolicy, append, rotateCount, formatter, level)
}

/**
 * A log handler that writes log entries into a file, and rolls this file
 * at a requested interval (hourly, daily, or weekly).
 */
class FileHandler(
    path: String,
    rollPolicy: Policy,
    val append: Boolean,
    rotateCount: Int,
    formatter: Formatter,
    level: Option[Level])
  extends Handler(formatter, level) {

  // This converts relative paths to absolute paths, as expected
  val (filename, name) = {
    val f = new File(path)
    (f.getAbsolutePath, f.getName)
  }
  val (filenamePrefix, filenameSuffix) = {<commit_msg>[split] util-logging: for file handlers, default to append=true since that was the default with FileHandlerConfig and is safer behavior<commit_after>/*
 * Copyright 2010 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twitter.logging

import com.twitter.util.{HandleSignal, StorageUnit, Time}
import java.io.{File, FilenameFilter, FileOutputStream, OutputStream}
import java.nio.charset.Charset
import java.text.SimpleDateFormat
import java.util.{Calendar, Date, logging => javalog}

sealed abstract class Policy
object Policy {
  case object Never extends Policy
  case object Hourly extends Policy
  case object Daily extends Policy
  case class Weekly(dayOfWeek: Int) extends Policy
  case object SigHup extends Policy
  case class MaxSize(size: StorageUnit) extends Policy
}

object FileHandler {
  val UTF8 = Charset.forName("UTF-8")

  /**
   * Generates a HandlerFactory that returns a FileHandler
   *
   * @param filename
   * Filename to log to.
   *
   * @param rollPolicy
   * When to roll the logfile.
   *
   * @param append
   * Append to an existing logfile, or truncate it?
   *
   * @param rotateCount
   * How many rotated logfiles to keep around, maximum. -1 means to keep them all.
   */
  def apply(
    filename: String,
    rollPolicy: Policy = Policy.Never,
    append: Boolean = true,
    rotateCount: Int = -1,
    formatter: Formatter = new Formatter(),
    level: Option[Level] = None
  ) = () => new FileHandler(filename, rollPolicy, append, rotateCount, formatter, level)
}

/**
 * A log handler that writes log entries into a file, and rolls this file
 * at a requested interval (hourly, daily, or weekly).
 */
class FileHandler(
    path: String,
    rollPolicy: Policy,
    val append: Boolean,
    rotateCount: Int,
    formatter: Formatter,
    level: Option[Level])
  extends Handler(formatter, level) {

  // This converts relative paths to absolute paths, as expected
  val (filename, name) = {
    val f = new File(path)
    (f.getAbsolutePath, f.getName)
  }
  val (filenamePrefix, filenameSuffix) = {
{'fraction_non_alphanumeric': 0.08523290386521308, 'fraction_numerical': 0.004757185332011893, 'mean_word_length': 3.604014598540146, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4555369', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1397, 'n_words': 578}