Scala模拟生成日志文件

Scala模拟生成日志文件,包括:IP,请求的网页,状态等

import java.io.{File, PrintWriter}
import java.text.SimpleDateFormat
import java.util.{Date, Random}

/**
 * @className: .SparkSteamingDemo02
 * @author: Star
 * @create: 2022-07-27 19:10
 */

object SparkSteamingDemo02 {
  def main(args: Array[String]): Unit = {
    val writer = new PrintWriter(new File("datas/9.txt"))
    for (i <- 1 to 10) {
      writer.println(loggers)
    }
    writer.flush()
    writer.close()
  }

  def loggers(): Unit = {
    var i = 0
    while (i <= 10) {
      ip
      web
      mpb
      logTime
      requestType
      addr
      status
      println(" ")
      i = i + 1
      //可以不要,为了便于观察而已
      Thread.sleep(1000)
    }
  }

  /**
   * 生成固定位数的IP
   */
  def ip(): Unit = {
    val random1 = new Random()
    val ran = random1.nextLong().toString
    val num = ran.substring(0, 13)
    val nums = num.substring(1, 4) + "." + num.substring(4, 7) + "." + num.substring(7, 10) + "." + num.substring(10, 13)
    print(" " + nums)
  }

  /**
   * 生成日志的时间;
   */
  def logTime(): Unit = {
    val date = new Date()
    var dateTime: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss")
    var logTimes = dateTime.format(date)
    print(" " + logTimes)
  }

  /**
   * 生成请求类型
   */
  def requestType(): Unit = {
    var num = scala.util.Random.nextInt(10)
    if (num <= 7) {
      print(" " + "POST")
    } else {
      print(" " + "GET")
    }
    if (num > 7) {
      print("null")
    }
  }

  /**
   * 随机生成手机品牌
   */
  def mpb(): Unit = {
    val phone = Array("华为", "IQOO", "小米", "苹果", "诺基亚", "OPPle", "努比亚", "一加", "三星", "锤子")
    var rand = scala.util.Random.nextInt(10)
    print(" " + phone(rand))
  }

  /**
   * 生成访问的页面
   */
  def addr(): Unit = {
    var str = scala.util.Random.nextInt(15)
    val page = Array(" " +
      "/www.wangxig.bi/69809567/jgfnblns/79/gooh.sc",
      "/php/1427679483/course/format/weeks/format.js",
      "/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple-min.css",
      "/favicon.ico HTTP/1.1",
      "/course/view.php?id=27",
      "https://blog.csdn.net",
      "www.https://ai.taobao.com",
      "http://toutiao.sogou.com",
      "https:www.hour//mini.yyrtv.com",
      "https://www.bilibili.com",
      "https://www.bilibili.com/video",
      "https://www.douyin.com",
      "http://picture.youth.cn",
      "https://blog.csdn.net",
      "https://daohang.qq.com"
    )
    print(" " + page(str))
  }

  /**
   * 生成状态码
   */
  def status(): Unit = {
    var str = scala.util.Random.nextInt(7)
    val sta = Array("200", "400", "404", "500", "900", " ", "401")
    print(" " + sta(str))
  }

  /**
   * 浏览器类型;
   */
  def web(): Unit = {
    val web1 = Array("QQ", "Internet explorer", "Safari", "Firefox", "Chrome", "Google", "Edge")
    val c = (Math.random() * (7) + 0).toInt
    print(" " + web1(c))
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值