RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-599005

Андрей Рацкевич's questions

Martin Hope
Андрей Рацкевич
Asked: 2025-01-26 03:25:27 +0000 UTC

编译 log4j.xml 文件时出现 Log4J 日志记录错误

  • 4

作为培训任务的一部分,我编写了一个文件 Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
    <appenders>
        <File name="ExceptionsFile" fileName="logs/errors.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </File>
        <File name="QueriesFile" fileName="logs/queries.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Filters>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </File>
    </appenders>
    <loggers>
        <logger name="Main" level="info" additivity="false">
            <appender-ref ref="QueriesFile"/>
        </logger>
        <logger name="Main" level="error" additivity="false">
            <appender-ref ref="ExceptionsFile"/>
        </logger>
        <root level="warn">
            <appender-ref ref="ExceptionsFile"/>
        </root>
    </loggers>
</configuration>

在pom.xml中连接库

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.18.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.18.0</version>
        </dependency>

将日志添加到主类

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Scanner;
public class Main {
    private static final Logger queryLogger = LogManager.getLogger("Main");
    private static final Logger errorLogger = LogManager.getLogger("Main");
    private static final String ADD_COMMAND = "add Василий Петров " +
            "[email protected] +79215637722";
    private static final String COMMAND_EXAMPLES = "\t" + ADD_COMMAND + "\n" +
            "\tlist\n\tcount\n\tremove Василий Петров";
    private static final String COMMAND_ERROR = "Wrong command! Available command examples: \n" +
            COMMAND_EXAMPLES;
    private static final String helpText = "Command examples:\n" + COMMAND_EXAMPLES;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        CustomerStorage executor = new CustomerStorage();
        while (true) {
            try {
                String command = scanner.nextLine();
                String[] tokens = command.split("\\s+", 2);
                if (tokens[0].equals("add")) {
                    executor.addCustomer(tokens[1]);
                    queryLogger.info("Added customer: {}", tokens[1]); // Логирование успешного добавления
                } else if (tokens[0].equals("list")) {
                    executor.listCustomers();
                } else if (tokens[0].equals("remove")) {
                    executor.removeCustomer(tokens[1]);
                    queryLogger.info("Removed customer: {}", tokens[1]); // Логирование удаления
                } else if (tokens[0].equals("count")) {
                    System.out.println("There are " + executor.getCount() + " customers");
                } else if (tokens[0].equals("help")) {
                    System.out.println(helpText);
                } else {
                    System.out.println(COMMAND_ERROR);
                    queryLogger.warn("Invalid command entered: {}", command); // Логирование неправильных команд
                }
            } catch (Exception e) { // Ловим общее исключение, лучше заменить на конкретные
                errorLogger.error("Error encountered: ", e); // Логирование ошибок
                System.out.println("Error encountered: " + e.getMessage());
            }
        }
    }
}

代码运行正常,训练验证自动测试通过,在日志文件夹中创建了两个日志文件错误和查询,但没有写入任何内容,它们保持为空。请告诉我为什么消息没有写入日志文件。

java
  • 1 个回答
  • 34 Views
Martin Hope
Андрей Рацкевич
Asked: 2024-07-30 04:20:10 +0000 UTC

Maven 找不到 Main 类的路径

  • 4

作为发展我使用 Maven 的技能的一部分,我编译了一个块,多亏了提示,错误消失了。但是当我在终端中运行 java -jar target/JavaStart-1.0-SNAPSHOT-jar-with-dependency.jar 时,出现以下消息:

无法找到或加载主类Main

这是我的 Maven:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>JavaStart</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>22</maven.compiler.source>
        <maven.compiler.target>22</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.10.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.7.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- Замените на ваш класс с методом main -->
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

请告诉我如何指示正确的路径?

<manifest>
                            <!-- Замените на ваш класс с методом main -->
                            <mainClass>Main</mainClass>
</manifest>

我只有一个带有 main 方法的 Main 类。或者路径应该包含计算机上本地文件夹的路径?

java
  • 1 个回答
  • 28 Views
Martin Hope
Андрей Рацкевич
Asked: 2024-06-22 00:28:28 +0000 UTC

抽象类和外部比较器的 Java 作业问题。我写了一个几乎可以工作的代码

  • 4

操作 在 AbstractClasses/practice_1 项目的 java_basics 存储库中运行作业。该目录是空的,您需要在其中创建一个新项目并完成任务。

  1. 创建一个包含员工的 Company 类并实现以下方法:

雇用一名员工 - 雇用(雇员员工),雇用员工列表 - 雇用所有(收集雇主),解雇一名员工 - 解雇(员工员工),获取公司的收入值 - getIncome()。每个方法都不应该有静态修饰符,这将允许 Company 类的每个对象拥有自己的一组员工、自己的收入计算、解雇和雇用。根据应用程序的逻辑选择方法的参数和返回值。

  1. 创建两个返回指定长度(计数)的列表的方法。它们应包含按工资降序和升序排序的员工:

列表 getTopSalaryStaff(int count),列表 getLowestSalaryStaff(int count)。

  1. 使用薪资信息和薪资条件创建员工类别:

经理 - 工资由固定部分和为公司赚到的钱的 5% 形式的奖金组成。随机生成 115,000 到 140,000 卢布之间的公司收入。 TopManager - 工资由固定部分和奖金组成,如果公司收入超过1000万卢布,则奖金为工资的150%。操作员——工资仅由固定部分组成。每个员工类必须实现 Employee 接口。 Employee 接口必须声明一个返回员工工资的方法 - getMonthSalary()。

根据工资逻辑选择方法的参数和返回值。在接口中声明必要的方法。要演示和测试您的课程如何运作:

为公司创建和雇用:180 名操作员、80 名经理销售经理、10 名顶级经理。打印出公司薪资最高的 10-15 名名单。打印出公司内 30 名最低工资的清单。解雇 50% 的员工。打印出公司薪资最高的 10-15 名名单。打印出公司内 30 名最低工资的清单。

还没有说您需要编写一个工作比较器。老实说,我用代码查看了选项,但我仍然找不到如何创建比较器,以便根据 TopManager 类中的条件授予奖金 if ( company.getCompanyRevenue() > 10_000_000.0) ,并且与现在不同的是,尽管 10_000_000.0 中的情况存在,但奖金是根据公司收入 800000.0 授予的。给我一个引导。

  1. 请告诉我如何在这里正确提问,这是我的第一次经验。
  2. 请提供解决方案。
import java.util.*;
public class Company {
    private double inCome = 0;
    List <Employee> employees = new ArrayList<>();
    public void  hire (Employee employee) {
        employees.add(employee);
    }
    public void  hireAll (List <Employee> employees) {
        for ( Employee e : employees) {
            hire(e);
        }
    }
    public void fire (Employee employee) {
        employees.remove(employee);
    }
    public double getInCome () {
        return inCome;
    }
    public List <Employee> getTopSalaryStaff (int count) {
        employees.sort(new SortByTopSalaryStaff());
        if (count < 0 && count > employees.size()) {
            List <Employee> EmptyList = new ArrayList<>();
            System.out.println("неверное количество");
            return EmptyList;
        }
        return employees.subList(0, count);
    }
    public List <Employee> getLowestSalaryStaff (int count) {
        employees.sort(new SortByLowestSalaryStaff());
        if (count < 0 && count > employees.size()) {
            List <Employee> EmptyList = new ArrayList<>();
            System.out.println("неверное количество");
            return EmptyList;
        }
        return employees.subList(0, count);
    }
    public double getCompanyRevenue () {

        for (Employee e : employees) {
            inCome = inCome + e.getRevenue();
        }
        return inCome;
    }

}

public interface Employee {
    double getMonthSalary ();
    double getRevenue();
}

import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        Company company = new Company();
        List <Employee> workers = new ArrayList<>();
        Operator operator = new Operator();
        //Manager manager = new Manager();
        //TopManager topManager = new TopManager(company);
        company.hire(operator);
        for (int i = 0; i < 180; i++){
            workers.add(new Operator());
        }
        for (int i = 0; i < 80; i++) {
            workers.add(new Manager());
            if ( i%8 == 0 ) {
                workers.add(new TopManager(company));
            }
        }
        company.hireAll(workers);

        System.out.println();
        System.out.println("Количество работников : " + company.employees.size());
        System.out.println();
        System.out.println("Доход компании : " + company.getCompanyRevenue());
        System.out.println();
        System.out.println("topSalaryStaff");
        System.out.println();

        List <Employee> topSalaryStaff = company.getTopSalaryStaff(12);
        for (Employee e: topSalaryStaff) {
            System.out.println(e.getMonthSalary());
        }
        System.out.println("--------------------------------------");
/*
        System.out.println();
        System.out.println("lowSalaryStaff");
        System.out.println();
        List <Employee> lowSalaryStaff = company.getLowestSalaryStaff(12);
        for (Employee e: lowSalaryStaff) {
            System.out.println(e.getMonthSalary());
        }
        System.out.println("--------------------------------------");


 */

    }
}

import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        Company company = new Company();
        List <Employee> workers = new ArrayList<>();
        Operator operator = new Operator();
        //Manager manager = new Manager();
        //TopManager topManager = new TopManager(company);
        company.hire(operator);
        for (int i = 0; i < 180; i++){
            workers.add(new Operator());
        }
        for (int i = 0; i < 80; i++) {
            workers.add(new Manager());
            if ( i%8 == 0 ) {
                workers.add(new TopManager(company));
            }
        }
        company.hireAll(workers);

        System.out.println();
        System.out.println("Количество работников : " + company.employees.size());
        System.out.println();
        System.out.println("Доход компании : " + company.getCompanyRevenue());
        System.out.println();
        System.out.println("topSalaryStaff");
        System.out.println();

        List <Employee> topSalaryStaff = company.getTopSalaryStaff(12);
        for (Employee e: topSalaryStaff) {
            System.out.println(e.getMonthSalary());
        }
        System.out.println("--------------------------------------");
/*
        System.out.println();
        System.out.println("lowSalaryStaff");
        System.out.println();
        List <Employee> lowSalaryStaff = company.getLowestSalaryStaff(12);
        for (Employee e: lowSalaryStaff) {
            System.out.println(e.getMonthSalary());
        }
        System.out.println("--------------------------------------");


 */

    }
}

public class Operator implements Employee {
    private  double salary = 30_000;
    final double income = 0;
    @Override
    public double getMonthSalary() {
        return salary;
    }
    @Override
    public double getRevenue() {
        return income;
    }
}
import java.util.Comparator;
public class SortByLowestSalaryStaff implements Comparator<Employee> {
    @Override
    public int compare(Employee employee1, Employee employee2) {
        if (employee1.getMonthSalary() > employee2.getMonthSalary()) {
            return 1;
        }
        if (employee1.getMonthSalary() == employee2.getMonthSalary()) {
            return 0;
        }
        return -1;
    }
}

import java.util.Comparator;
public class SortByTopSalaryStaff implements Comparator <Employee> {

    @Override
    public int compare(Employee employee1, Employee employee2) {

        return Double.compare(employee2.getMonthSalary(), employee1.getMonthSalary());
    }





    /*
    @Override
    public int compare(Employee employee1, Employee employee2) {

        if (employee1.getMonthSalary() < employee2.getMonthSalary()) {
            return 1;
        }
        if (employee1.getMonthSalary() == employee2.getMonthSalary()) {
            return 0;
        }
        return -1;
    }

     */


}

import java.util.Random;
public class TopManager implements Employee{
    private  final Company company;
    private final double salary;
    private final double bonus = 1.5;
    private final double earnedLimit = 10_000_000.0;
    final double income = 0;
    public TopManager (Company company) {
        this.company = company;
        this.salary = 900000; // + new Random().nextInt(40000);
    }
    @Override
    public double getRevenue() {
        return income;
    }
    @Override
    public double getMonthSalary() {
       //double companyIncome = company.getCompanyRevenue();

        if ( company.getCompanyRevenue() > earnedLimit) {
            return  salary + salary * bonus;
        }
        return salary;
    }
}
java
  • 1 个回答
  • 52 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5