{"id":17154191,"url":"https://github.com/gurleensethi/loggerpreferences","last_synced_at":"2025-07-20T07:34:53.168Z","repository":{"id":98501543,"uuid":"126694091","full_name":"gurleensethi/LoggerPreferences","owner":"gurleensethi","description":"Get to know which class changed the value in SharedPreferences.","archived":false,"fork":false,"pushed_at":"2018-03-25T15:28:21.000Z","size":165,"stargazers_count":21,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T12:52:12.030Z","etag":null,"topics":["android","android-development","android-library","java","kotlin","kotlin-android"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gurleensethi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-03-25T12:14:00.000Z","updated_at":"2024-07-24T00:33:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"cbe5766d-70f3-498a-ac75-4c3dbc5e2d08","html_url":"https://github.com/gurleensethi/LoggerPreferences","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/gurleensethi/LoggerPreferences","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurleensethi%2FLoggerPreferences","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurleensethi%2FLoggerPreferences/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurleensethi%2FLoggerPreferences/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurleensethi%2FLoggerPreferences/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gurleensethi","download_url":"https://codeload.github.com/gurleensethi/LoggerPreferences/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gurleensethi%2FLoggerPreferences/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266086195,"owners_count":23874486,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-development","android-library","java","kotlin","kotlin-android"],"created_at":"2024-10-14T21:48:31.781Z","updated_at":"2025-07-20T07:34:53.139Z","avatar_url":"https://github.com/gurleensethi.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![](https://jitpack.io/v/gurleensethi/LoggerPreferences.svg)](https://jitpack.io/#gurleensethi/LoggerPreferences)\n# LoggerPreferences\nGet to know which class changed the value in SharedPreferences.\n\n## Installation\nIn the root level `build.gradle` file add:\n```gradle\nallprojects {\n    repositories {\n        ...\n        maven { url 'https://jitpack.io' }\n    }\n}\n```\nNow add the dependency:\n```gradle\ndependencies {\n    compile 'com.github.gurleensethi:LoggerPreferences:v1.0.0'\n}\n```\n## Usage\n### Initialize\nLoggerPreferences will log any action that is taken with `SharedPreferences`, be it changing values or retrieving them.\nTo enable logging, you have to initalize LoggerPreferences.\n```java\nLoggerPreferences.init(true, true);\n```\nThe first parameter tells LoggerPreferences to log everytime when a value is changed in `SharedPreferences` and the second parameter tells to log everytime a value is retrieved.\n\n### Getting a Reference\nThere are two ways you can obtain a reference.\nFirst is by providing a `Context`, filename (`Stirng`) and mode (`int`).\n```java\n//LoggerPreferences.get(Context, fileName, mode);\nLoggerPreferences preferences = LoggerPreferences.get(this, \"sp_file\", Context.MODE_PRIVATE)\n        .with(this);\n```\nSecond is by providing an object of `SharedPreferences` directly.\n```java\n//LoggerPreferences.get(SharedPreferences);\nLoggerPreferences preferences = LoggerPreferences.get(getSharedPreferences(\"sp_file\", Context.MODE_PRIVATE))\n        .with(this);\n```\nYou also have to call the function `with(Object)` and pass in the object which is responsible for interacting(changing/retrieving) with `SharedPreferences`. `with` takes in a parameter of type `Object`.\nIf you don't call `with()` then by default `LoggerPreferences` will logged as the file name, so make sure to call `with()`.\n\n### API\nLoggerPreferences is a wrapper around `SharedPreferences`. It has all the functions provided by `SharedPreferences` and works in exactly the same manner.\n\nSo if in the `MainActivity` you change a `String` value.\n```java\npreferences.edit()\n        .putString(\"key_string\", \"new value\")\n        .apply();\n```\nIn `logcat` you will get:\n```\nD/LoggerPreferences: [String] MainActivity changed the value of Key[key_string] from [old value] to [new value].\n```\nIf you are retrieving a `String` value:\n```java\nString value = preferences.getString(\"key_string\", null);\n```\nIn `logcat` you will get:\n```\nD/LoggerPreferences: [String] MainActivity is retrieving the value of Key[key_string] which is set to [new value].\n```\n\nLoggerPreferences has all the functions provided by `SharedPreferences` and `SharedPreferences.Editor`.\n```java\npublic class MainActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        LoggerPreferences.init(true, true);\n\n        LoggerPreferences preferences = LoggerPreferences.get(this, \"sp_file\", Context.MODE_PRIVATE)\n                .with(this);\n\n        HashSet\u003cString\u003e set = new HashSet\u003c\u003e();\n        set.add(\"New Value\");\n        set.add(\"Oh!\");\n        set.add(\"I am in a set\");\n\n        preferences.edit()\n                .putInt(\"key_int\", 123)\n                .putString(\"key_string\", \"new value\")\n                .putBoolean(\"key_boolean\", false)\n                .putFloat(\"key_float\", 10.0f)\n                .putLong(\"key_long\", 112320L)\n                .putStringSet(\"key_set\", set)\n                .apply();\n\n        preferences.getString(\"key_string\", null);\n        preferences.getInt(\"key_int\", 0);\n        preferences.getBoolean(\"key_boolean\", false);\n        preferences.getLong(\"key_long\", 1000L);\n        preferences.getFloat(\"key_float\", 10f);\n        preferences.getStringSet(\"key_set\", null);\n        preferences.getAll();\n        preferences.registerOnSharedPreferenceChangeListener(null);\n        preferences.unregisterOnSharedPreferenceChangeListener(null);\n    }\n}\n```\nLogcat:\n```\nD/LoggerPreferences: [Int] MainActivity changed the value of Key[key_int] from [123] to [123].\nD/LoggerPreferences: [String] MainActivity changed the value of Key[key_string] from [new value] to [new value].\nD/LoggerPreferences: [Boolean] MainActivity changed the value of Key[key_boolean] from [true] to [false].\nD/LoggerPreferences: [Float] MainActivity changed the value of Key[key_float] from [1.0] to [10.0].\nD/LoggerPreferences: [Long] MainActivity changed the value of Key[key_long] from [10000] to [112320].\nD/LoggerPreferences: [Set\u003cString\u003e] MainActivity changed the value of Key[key_set] from [[Test, Testing!]] to [I am in a set,Oh!,New Value].\n\nD/LoggerPreferences: [String] MainActivity is retrieving the value of Key[key_string] which is set to [new value].\nD/LoggerPreferences: [Int] MainActivity is retrieving the value of Key[key_int] which is set to [123].\nD/LoggerPreferences: [Boolean] MainActivity is retrieving the value of Key[key_boolean] which is set to [false].\nD/LoggerPreferences: [Long] MainActivity is retrieving the value of Key[key_long] which is set to [112320].\nD/LoggerPreferences: [Float] MainActivity is retrieving the value of Key[key_float] which is set to [10.0].\nD/LoggerPreferences: [Set\u003cString\u003e] MainActivity is retrieving the value of Key[key_set] which is set to [I am in a set,Oh!,New Value].\nD/LoggerPreferences: MainActivity is retrieving all values.\nD/LoggerPreferences: MainActivity has registered a OnSharedPreferenceChangeListener.\nD/LoggerPreferences: MainActivity has unregistered a OnSharedPreferenceChangeListener.\n```\n\n### Tips\nLoggerPreferences uses the same tag when logging: `LoggerPreferences`. In your logcat search `LoggerPreferences` to get all the logs.\n\n\u003cimg src=\"https://raw.githubusercontent.com/gurleensethi/LoggerPreferences/master/images/logger_tag.png\" width=\"350\"/\u003e\n\n#### Never log in production!\n\n## Support\nIf you have any idea or need a change in the library or found a bug, please [open an issue](https://github.com/gurleensethi/LoggerPreferences/issues/new).\n\n## License\n```txt\nCopyright 2018 Gurleen Sethi\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR\nANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgurleensethi%2Floggerpreferences","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgurleensethi%2Floggerpreferences","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgurleensethi%2Floggerpreferences/lists"}